How to add notification badges to other menu items

Is there a hook that allows me to add these notification badges to custom menu items? I am using Better Messages for my messaging system, but it doesn’t show in the menu when there are unread messages. I tried adding a shortcode to the menu item via hivepress/v1/menus/user_account, but it sanitizes the label and prevents it from being used. Any suggestions?

Okay, I’ve made some progress debugging the hook’s properties and saw that the meta property is the count. So I added my unread messages count there, which now works for the menu drop down and the side bar, but it still does not show up in the top menu next to the username like the Bookings one does.

Hi,

Please make sure that you have the correct function added to this hook and that it is triggered more than once, because the correct meta parameter is the one that accepts the number of notifications in the menu item.

I don’t understand. This is my code (manually entering 1 unread message):

add_filter( 'hivepress/v1/menus/user_account', [ $this, 'account_sidebar' ], 10, 2 );
public function account_sidebar( $props, $menu ) {
    $props[ 'items' ][ 'messages' ] = [
        'label'  => esc_html__( 'Messages', 'hivepress' ),
        'url'    => '/account/messages/',
        '_order' => 30,
        'meta'   => 1
    ];
    return $props;
} // End account_sidebar()

How do I make sure that it’s triggered more than once? What am I doing wrong?

Hi,

Everything should appear in the header and in the drop-down menu, since this filter is suitable for two displays. Please disable third-party plugins and customizations (if there are any) and check if this issue persists. If you use a caching plugin, make sure that caching is disabled for logged-in users. Also, I recommend checking the settings of your hosting provider, as they may also have caching.

Unfortunately, I disabled all of my custom code less the snippet I showed you, and deactivated all plugins except the main Hivepress plugin, and it’s still behaving the same. What’s interesting is that the booking notifications show up next to the username up top just fine, but not from my snippet.

@andrii , alright, man. I went through all of that debugging for nothing. I finally downloaded the bookings extension and found the correct hook for the notice by the username.

Here is my code for anyone else that might need it. This only adds to the total count at top. You still have to use the other snippet from above for the drop down menu and sidebar. Obviously you’ll need to get the count using your own method, but the most important part is adding the count to the notice_count property of the $context array.

add_filter( 'hivepress/v1/components/request/context', [ $this, 'set_request_context' ], 100 );
public function set_request_context( $context ) {
    // Get the unread message count
    $unread_messages = SL_COMMUNICATIONS::new_message_menu_indicator();

    // If we have some
    if ( isset( $unread_messages[ 'count' ] ) && absint( $unread_messages[ 'count' ] ) > 0 ) {

        // Add value to context array in case we need it
        $context[ 'better_messages_count' ] = absint( $unread_messages[ 'count' ] );

        // And the count to the current count
        $context[ 'notice_count' ] += absint( $unread_messages[ 'count' ] );
    }

    // Always return context
    return $context;
} // End set_request_context()
1 Like

Hello @apos37 !

Thanks for helping the comminuty!
I’ve been trying to achive the same integration with Better Messages for quite some time.
I’ve been trying out the code above, pasting it in my functions.php in my ChildTheme, but I get a lot of errors. Can you help me out, and tell me where to put the code, and also if anything else needs to change?

I also found this short code from Better Messages, to get the count. But you don’t use that?
[better_messages_unread_counter hide_when_no_messages=“1” preserve_space=“1”]

Thank you!!!

Hello, @aming4stars. Glad to help where I can. :slight_smile:

I don’t know what kind of errors you’re getting, but I can make some assumptions… If you are just copying and pasting the code above, it won’t work. It was just a demonstration of how I used it.

  1. First off, since you are adding code to functions.php (which is fine to do), you will need to adjust the code to not be in a class. So remove the “public” from “public function,” and change the filter’s callback from [ $this, 'callback' ] to just 'callback', and with a more unique function name:
add_filter( 'hivepress/v1/menus/user_account', 'apos37_account_sidebar' , 10, 2 );
function apos37_account_sidebar()...
  1. The $unread_messages variable in the second code snippet I shared uses a custom function that I made to include a custom count. I honestly couldn’t tell you what was in the function exactly, because I have since deleted Better Messages for its lack of reliability when it came to email notifications. Sorry, I also deleted all of my code associated with it, too. I ended up creating my own messaging system instead. Anyway, if you want to get this to work, you’d have to look into getting the message count a different way. I do remember looking at the Better Messages plugin source code for the shortcode you shared to get the function for the count. Once you get the count, your final code should look something like:
add_filter( 'hivepress/v1/menus/user_account', 'apos37_account_sidebar' , 10, 2 );
function apos37_account_sidebar( $props, $menu ) {
    $props[ 'items' ][ 'messages' ] = [
        'label'  => esc_html__( 'Messages', 'hivepress' ),
        'url'    => '/account/messages/',
        '_order' => 30,
        'meta'   => 1
    ];
    return $props;
} // End apos37_account_sidebar()

add_filter( 'hivepress/v1/components/request/context', 'apos37_set_request_context', 100 );
function apos37_set_request_context( $context ) {
    // Get the unread message count
    $unread_messages = the_function_used_to_get_better_messages_count();

    // If we have some
    if ( absint( $unread_messages > 0 ) {

        // And the count to the current count
        $context[ 'notice_count' ] += absint( $unread_messages );
    }

    // Always return context
    return $context;
} // End apos37_set_request_context()

Sorry I can’t be more help here.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.