Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save champsupertramp/d13302363caa36f2ae8a9281208d5b12 to your computer and use it in GitHub Desktop.
Save champsupertramp/d13302363caa36f2ae8a9281208d5b12 to your computer and use it in GitHub Desktop.
Ultimate Member 2.0 - Real-time notification - Add custom notification type
/*
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()->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()->api()->store_notification( um_profile_id(), 'new_action', $vars );
}
}
@spiderwisp
Copy link

It's worth mentioning that once you create a custom notification, you must then enable it in the backend. Ultimate Member -> Extensions -> Notifications -> check the box to enable your custom action (and optionally add a message).

@iamtoft
Copy link

iamtoft commented Jan 11, 2022

So, how could i trigger the notifications, when the user uploads own "blog post"?

@champsupertramp
Copy link
Author

Hi @iamtoft

Did you mean when someone created a post? If so, you can use this action hook to trigger the notification:

add_action("wp_insert_post","um_011122_send_notification_on_post_created");
function um_011122_send_notification_on_post_created( $post_id ){
   // Add the custom notification here.
}

Regards,

@iamtoft
Copy link

iamtoft commented Jan 11, 2022

Hi @champsupertramp

So, i need to send a notification to the user who created the post, when the post is published. Im using automation to create dynamic images and need to show a notification when the image is done, and ready to watch

@NicoSteyl
Copy link

How can I leverage this to display order information for Woocommerce? When a order is placed, the customer receives an email. I basically want the email subject to appear in the notifications when they make a purchase or place an order... PLEASE HELP!

@dlv2372
Copy link

dlv2372 commented Feb 14, 2023

For those who want to use it to use it to display Order Status Changes ( Woocommerce ) in the Ultimate Member Notification center : paste this in your functions.php :

Then, enable it in the backend. Ultimate Member -> Extensions -> Notifications ->
And change the "Template" textbox to something like :

Your order status has changed to {status}. Order number: {order_number}

`// Add custom notification type to UM
add_filter('um_notifications_core_log_types', 'add_order_notification_type', 200 );
function add_order_notification_type( $array ) {
$array['order_status_changed'] = array(
'title' => 'Order Status Changed',
'template' => 'Your order status has changed to {status}. Order number: {order_number}',
'account_desc' => 'Receive notifications when the status of your order changes',
);
return $array;
}

// Add custom icon to notification
add_filter('um_notifications_get_icon', 'add_order_notification_icon', 10, 2 );
function add_order_notification_icon( $output, $type ) {
if ( $type == 'order_status_changed' ) {
$output = '';
}
return $output;
}

// Trigger notification on order status change
add_action( 'woocommerce_order_status_changed', 'trigger_order_status_notification', 10, 4 );
function trigger_order_status_notification( $order_id, $status_from, $status_to, $order ) {
$user_id = $order->get_user_id();
$order_number = $order->get_order_number();

if ( ! $user_id ) {
    return;
}

$vars = array(
    'status' => $status_to,
    'order_number' => $order_number
);

UM()->Notifications_API()->api()->store_notification( $user_id, 'order_status_changed', $vars );

}`

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