How to Display unread message count for users

I know that there’s a shortcode that dose shows how many unread messages a user has
but how can I display this in the account menu
shortcodes don’t work in functions :frowning:
Are there any hooks that can function the same as this shortcode?

[better_messages_unread_counter hide_when_no_messages="1" preserve_space="1"]

I wanted to solve this by adding the shortcode to the function and using the do_shortcode

add_filter(
    'hivepress/v1/menus/user_account',
    function( $menu ) {

        $unread_messages_shortcode = '[better_messages_unread_counter hide_when_no_messages="1" preserve_space="1"]';

 
        $unread_messages_counter = do_shortcode($unread_messages_shortcode);

   
        $chat_label = 'Messges ' . $unread_messages_counter;

  
        $menu['items']['chat'] = [
            'label'  => $chat_label,
            'url'    => 'https://my-site.com/chat',
            '_order' => 20,
        ];

        return $menu;
    }
);

Related posts

I have tried using a function that will inject the shortcode into the fucnation

add_filter('hivepress/v1/menus/user_account', function ($menu) {
    $chat_label = 'הודעות <span id="unread-messages-counter"></span>';

    $menu['items']['chat'] = [
        'label'  => $chat_label,
        'url'    => 'https://workway.co.il/chat',
        '_order' => 20,
    ];

    return $menu;
});

function enqueue_custom_script() {
   
    wp_enqueue_script('custom-script', '/custom-css-js/messges.js', array('jquery'), null, true);

    wp_localize_script('custom-script', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
}
add_action('wp_enqueue_scripts', 'enqueue_custom_script');

add_action('wp_ajax_get_unread_messages_count', 'handle_unread_messages_count');
add_action('wp_ajax_nopriv_get_unread_messages_count', 'handle_unread_messages_count');
function handle_unread_messages_count() {
    echo do_shortcode('[better_messages_unread_counter hide_when_no_messages="1" preserve_space="1"]');
    wp_die();
}

the JS

jQuery(document).ready(function($) {
    $.ajax({
        url: ajax_object.ajax_url,
        type: 'POST',
        data: {
            action: 'get_unread_messages_count',
        },
        success: function(response) {
            $('#unread-messages-counter').html(response);
        }
    });
});

But it didn’t work

Hi,

Unfortunately, we cannot provide information about third-party plugins, so I recommend contacting its developer to find out how to get the count. Regarding the count display, if the menu item of this plugin is already added to the account using the account menu hook, you can pass this to the meta parameter in the item, and this count will be displayed in a red circle.