Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save champsupertramp/b752e0dc8c2219d38a9e2a70b397bab2 to your computer and use it in GitHub Desktop.
Save champsupertramp/b752e0dc8c2219d38a9e2a70b397bab2 to your computer and use it in GitHub Desktop.
Ultimate Member - Real-time notifications - Shortcode for Menus
<?php
/**
* Add notification count tag value
* {notification_count}
*/
add_filter( ‘um_profile_tag_hook__notification_count’,‘um_custom_notification_count’, 10, 2 );
function um_custom_notification_count( $value, $user_id ){
$count = UM()->Notifications_API()->api()->get_notifications( 0, 'unread', true );
return '<span class="um-notification-unreaditems count-'. $count . '">' . ( ( $count > 10 ) ? 10 . '+' : $count ) . '</span>';
}
/**
* Add new tags pattern
*/
add_filter( 'um_allowed_user_tags_patterns', 'um_custom_notification_tags', 10, 1 );
function um_custom_notification_tags( $tags ) {
$tags[ ] = '{notification_count}';
return $tags;
}
@AaronChaos
Copy link

Could you confirm if this compatible with the latest version of UM and the notification, add-on, please? I have included the code in my functions.php, however, the shortcode doesn't output anything when added to the menu.

@champsupertramp
Copy link
Author

Hi @AaronChaos

You can try this one with the Menu tag {notification_count}

add_filter( 'wp_nav_menu_items', 'um_051821_notification_count', 10, 2 );
function um_051821_notification_count( $items, $args ) {
    
    if( is_user_logged_in() && class_exists("UM_Notifications_API") ){
            $count = UM()->Notifications_API()->api()->get_notifications( 0, 'unread', true );	
            $count = '<span class="um-notification-unreaditems count-'. $count . '">' . ( ( $count > 10 ) ? 10 . '+' : $count ) . '</span>';
            $items = str_replace( '{notification_count}', $count, $items );
     }

    return $items;
}

Regards,

@AaronChaos
Copy link

Hi @champsupertramp, that worked perfectly, thank you.

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