I believe that the only sensible place to rename a menu item is the add_menu_classes
filter hook.
It has a different destiny, but I can’t see any other place.
Hook passes an array of first-level menu items as a parameter to the function.
The array elements look like this:
$menu = [
5 => Array (
[0] => 'Menu title'
[1] => 'capabilities'
[2] => 'menu_slug' // e.g. edit.php | edit.php?post_type=page | edit.php?post_type=cpt_slug
[3] => 'Page title'
[4] => 'css classes'
[5] => ??
[6] => 'dash-icon'
),
70 => Array (
[0] => 'Users'
[1] => 'list_users'
[2] => 'users.php'
[3] => ''
[4] => 'menu-top menu-icon-users'
[5] => 'menu-users'
[6] => 'dashicons-admin-users'
),
]
You can add badge with number by editing the menu item title, which is at index [0]
.
add_filter( 'add_menu_classes', 'se414500_menu_classes' );
function se414500_menu_classes ($menu)
{
// --- v1 ---
$my_counter = count( $something );
foreach($menu as &$item)
{
// use menu_slug of your item
if ($item[2] != 'edit.php?post_type=my_cpt_slug')
continue;
$item[0] .= sprintf(
'<span class="update-plugins count-%s"><span class="update-count">%s</span></span>',
$my_counter,
number_format_i18n( $my_counter )
);
}
// --- v2 ---
// foreach($menu as &$item)
// {
// // use menu_slug of your item
// if ($item[2] == 'edit.php?post_type=my_cpt_slug') {
// //
// // get the value to display next to the name
// $my_counter = count( $something );
//
// $item[0] .= sprintf(
// '<span class="update-plugins count-%s"><span class="update-count">%s</span></span>',
// $my_counter,
// number_format_i18n( $my_counter )
// );
// }
// }
return $menu;
}