Forked from ultimatemember/gist:5f725bff6bcf79d2988e
Last active
February 14, 2023 14:37
-
-
Save champsupertramp/d13302363caa36f2ae8a9281208d5b12 to your computer and use it in GitHub Desktop.
Ultimate Member 2.0 - Real-time notification - Add custom notification type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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();
}`