Skip to content

Instantly share code, notes, and snippets.

@ultimatemember
Last active January 22, 2020 19:00
Show Gist options
  • Save ultimatemember/5f725bff6bcf79d2988e to your computer and use it in GitHub Desktop.
Save ultimatemember/5f725bff6bcf79d2988e to your computer and use it in GitHub Desktop.
Using notifications API to add custom notifications
/*
This code sample shows you how to use the API to create
and add custom notifications (for real-time notifications)
plugin.
STEP 1: You need to extend the filter: um_notifications_core_log_types with your
new notification type as follows for example
*/
add_filter('um_notifications_core_log_types', 'add_custom_notification_type', 200 );
function add_custom_notification_type( $array ) {
$array['new_action'] = array(
'title' => 'When something happens', // title for reference in backend settings
'template' => '<strong>{member}</strong> has just did some action.', // the template, {member} is a tag, this is how the notification will appear in your notifications
'account_desc' => 'When member does some action on my profile', // title for account page (notification settings)
);
return $array;
}
/*
STEP 2: Add an icon and color to this new notification type
*/
add_filter('um_notifications_get_icon', 'add_custom_notification_icon', 10, 2 );
function add_custom_notification_icon( $output, $type ) {
if ( $type == 'new_action' ) { // note that our new action id is "new_action" from previous filter
$output = '<i class="um-icon-android-person-add" style="color: #336699"></i>';
}
return $output;
}
/*
STEP 3: Now you just need to add the notification trigger when a user does some action on
another user profile, I assume you can trigger that in some action hooks
for example when user view another user profile you can hook like this
basically you need to run this in code
$who_will_get_notification : is user ID who will get notification
'new_action' is our new notification type
$vars is array containing the required template tags, user photo and url when that notification is clicked
$um_notifications->api->store_notification( $who_will_get_notification, 'new_action', $vars );
*/
add_action('um_before_profile_fields', 'trigger_new_notification', 100);
function trigger_new_notification( $args ) {
global $um_notifications;
if ( is_user_logged_in() && get_current_user_id() != um_profile_id() ) {
um_fetch_user( get_current_user_id() );
$vars['photo'] = um_get_avatar_url( get_avatar( get_current_user_id(), 40 ) );
$vars['member'] = um_user('display_name');
$vars['notification_uri'] = um_user_profile_url();
$um_notifications->api->store_notification( um_profile_id(), 'new_action', $vars );
}
}
@hirenshah
Copy link

Do you have anything to trigger email reminders for missed notifications?

@hirenshah
Copy link

Do you have anything to trigger email reminders for missed notifications?

@champsupertramp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment