Custom Email Notifications

-
97890297_256118298835674_7704275922832064512_n-2

Hello UM Devs! I hope that you’re all safe and staying healthy! Here’s another Ultimate Member tutorial that will make your site more powerful!

Do you want to notify someone or yourself when someone logs in to your site? Or when someone created a porn group with UM Groups extension and you want to be the first one to check it out and delete it immediately!

In this tutorial, you will learn how to add custom email notifications with UM Email API as well as attaching a notification to an event or activity on your site!

Notify a user when someone logs in to the site

There are instances that we require our site to track users who log in and determine the time they access it via Email notifications. In this example, we will add a notification to notify us when someone logs in to our site.

Using the UM filter hook um_email_notifications we can register a new Email notification settings in WP Admin > Ultimate Member > Emails. The following code snippet is an example of how to register an email notification settings.

 function um_custom_email_notifications( $notifications ){

	$notifications['ninja_new_user_logged_in'] = array(
		'key'           	=> 'ninja_new_user_logged_in',
		'title'         	=> __( 'Ninja - New user has logged in','um-groups' ),
		'subject'       	=> '{site_name} - Someone logged-in to your site',
		'body'          	=> 'Hi,<br /><br />'.
		                   '"{display_name}" has access to your site right now.<br /><br />'.
		                   'To view profile, please click the following link: {user_profile_url}',
		'description'   	=> __('Whether to send the user an email when someone login to the site.','ultimate-member'),
		'recipient'   	 	=> 'admin',
		'default_active' 	=> true
	);

	return $notifications;
}
add_filter( 'um_email_notifications', 'um_custom_email_notifications', 10, 1 );

Here are the parameters:

key – This is a unique idenfier. It’s best that you add a custom prefix so that it won’t create a conflict with Ultimate Member email notifications. In the example, we use the prefix name ninja_

title – Add a title to your custom notification

subject – this is a subject of the email notification. You can use the built-in subject tags such as {site_name} to display the current name of your site. You can add custom tags in the next code example.

body – This where you add that appealing email body! By default, it supports HTML elements so you can be so creative adding those <markey> tags.

description – This is useful to remind you on something about this custom notification!

recipient – Leave it as is, we are trying to hack through the UM plugin to make the custom notification work! Kidding aside, we want to make this email notification visible to Admin only and we will set the admin/recipient email in the next code example.

default_active – This option is set to true but we have to ensure that this notification is turned on in UM Email Settings. Go to WP Admin > Ultimate Member > Settings > Email. See the screenshot below:

Ensure that you’ve turned it on and shows a “green check icon” otherwise the notification won’t be sent.

Now that you’ve registered your custom Notification settings, you can now attach that notification to an event or activity on your site. In the next code example, we will attach the email notification when a user logs in to the site.

add_action("wp_login","um_notify_when_someone_logged_in",10, 2);
function um_notify_when_someone_logged_in( $user_login, $user ){
    $user_id = $user->ID;
    um_fetch_user( $user_id ); 
    $recipient = "[email protected]";
	$profile_name = um_user("display_name");
	$profile_url = um_user_profile_url( $user_id );

	UM()->mail()->send( $recipient, 'ninja_new_user_logged_in', array(
				'plain_text'	 => 1,
				'tags'				 => array(
					'{profile_name}',
					'{profile_url}',
				),
				'tags_replace' => array(
					$profile_name,
					$profile_url,
				)
	) );
}

Above code details:

We have attached the email notification to the action hook wp_login which is added to the WordPress core. This is triggered after a user succesfully login to the site via UM login form or WP Admin > Login form.

You will notice that we have added two new tags for the email body:

{profile_name} – This is the current user’s display name.

{profile_url} – The URL of the user’s UM profile.

If you require more tags, you just need to add your custom pattern as follows:

....
'tags' => array(
	'{user_avatar}',
),
'tags_replace' => array(
	"<img src='".um_get_user_avatar_url( $user->ID )."'/>",
)
...

Just make sure that it matches the position of the array index when you merge it – see example below:

'tags' => array(
   '{profile_name}',
   '{user_avatar}',
   '{profile_url}',
),
'tags_replace' => array(
   $profile_name,
    "<img src='".um_get_user_avatar_url( $user->ID )."'/>",
   $profile_url,
)

Also, don’t forget to add the email template name ninja_new_user_logged_in that we registered in the first code example above. Here’s the code part that triggers the email notification with the example template name.:

UM()->mail()->send( $recipient, 'ninja_new_user_logged_in', array(
  ...
));

Change the example$recipient value to your admin email or personal email!

– A friendly reminder from a Ninja

And there you go! That’s how you use the UM Email Utility with settings! Very easy with extra steps!

I hope this helps you bringing your UM-powered website to be more powerful and robust!

If you find this helpful, please subscribe to my newsletter to get updates of my new tutorials! Don’t forget to leave some good vibes in the comment section below! Don’t hesitate to ask for any technical concerns! Peace!

Subscribe to my newsletter!



Help me reach other people who need the help

2 Comments Custom Email Notifications

Leave a Reply

Your email address will not be published. Required fields are marked *