4

I've looked and have not found anything that works.

I am trying to include a shortcode in a menu item's 'Navigation Label'. Basically Iwould like to have a dynamic title displaying the user's name & avatar.

[profilepicture round] [user_name]

Right now it's just spit back out since Wordpress Menus do not parse shortcodes. I've tried:

function my_nav_menu_objects_shortcode_mangler($items) {
    foreach ($items as $item) {
       $item->title = do_shortcode($item->title);
    }
    return $items;
}

But it doesn't parse it. I also tried $item->type_label with no luck. Am I just referencing the wrong attribute of item?

Also tried the following with no luck.

 add_filter('wp_nav_menu_items', 'do_shortcode');

The avatar and username can both be retrieved via php if needed:

<?php echo userpro_profile_data('user_login', $user_id); ?>
<?php echo userpro_profile_data('profilepicture', $user_id); ?>
  • This could be theme specific if your theme uses a custom nav menu walker. Can you confirm whether it uses the default WP nav menu walker? – cfx Jul 6 '14 at 8:02
  • Also, what kind of menu item is it (link? page? post? other?) and where are you entering the shortcode (as the nav label? title? other?)? – cfx Jul 6 '14 at 8:09
2

First of all download 'Shortcodes in Menus' plugin and install/activate it.

Then in theme's function.php add following code.

add_shortcode( 'current-username' , 'ss_get_current_username' );
function ss_get_current_username(){
    $user = wp_get_current_user();
    return $user->display_name;
} 

Now, in menu's navigation lable write [current-username] shortcode.

Same thing you can do for displaying avatar.

This way it will display logged in user name in menu item.

1

Since WP 4.4.0 you can use the following:

add_filter('nav_menu_item_title', function ($title, $item, $args, $depth) {
    return do_shortcode($title);
}, 10, 4);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.