4

I'm using Wordpress with UserPro, and want my menu to display the logged-in user's first name, linked to the user profile page.

The problem is that in my menu structure, the "Profile" menu option is supposed to have a sub-menu containing "edit profile", "submit", and "logout".

This is the code I'm currently using:

/*earlier code, currently commented out, for function to
display username in menu using #profile_name# placeholder

function give_profile_name($atts){
echo userpro_profile_data('first_name', get_current_user_id());
}

add_shortcode('profile_name', 'give_profile_name');

add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
    foreach ( $menu_items as $menu_item ) {
        if ( '#profile_name#' == $menu_item->title ) {
            global $shortcode_tags;
            if ( isset( $shortcode_tags['profile_name'] ) ) {
                // Or do_shortcode(), if you must.
                $menu_item->title = call_user_func( $shortcode_tags['profile_name'] );
            }    
        }
    }

    return $menu_items;
}  

end of earlier code */

//currently in use, unlinked code

    add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
    function my_custom_menu_item($items)
    {
        if(is_user_logged_in())
        {
            $user=wp_get_current_user();
            $name=$user->user_firstname;
            $items .= '<li>'.$name.'';
        }
        return $items;
    }

I can fiddle around and try to add the sub-menu under the menu by fiddling with the code from Firebug, but that would mean manual edits to all the links in the functions.php, which would be tedious.

I want to be able to add, edit, remove, and redirect the sub-menu items easily via the Wordpress menu.

Please advise.

7

Okay, I found a solution (and it can be used for any theme, with any plugin as it only uses core WordPress functions).

In the menu, name the menu item where you want the user's name to appear with a place-holder (see screenshot below). Example: #profile_name#, #user#, #random#

enter image description here

Now, add the following code to the your child-theme's functions.php:

function give_profile_name($atts){
    $user=wp_get_current_user();
    $name=$user->user_firstname; 
    return $name;
}

add_shortcode('profile_name', 'give_profile_name');

add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
    foreach ( $menu_items as $menu_item ) {
        if ( '#profile_name#' == $menu_item->title ) {
            global $shortcode_tags;
            if ( isset( $shortcode_tags['profile_name'] ) ) {
                // Or do_shortcode(), if you must.
                $menu_item->title = call_user_func( $shortcode_tags['profile_name'] );
            }    
        }
    }

    return $menu_items;
} 

In case you're using your own place-holder, remember to replace #profile_name# with the name of your custom place-holder in the code above.

Apologies in case I've misused the term 'place-holder'.

5

The code:

function give_profile_name($atts){
...
}

gives a warning. Better is:

function give_profile_name(){
...
}

And it is better to detect if the user is logged in or not. Like so:

function give_profile_name(){
    $user=wp_get_current_user();
    if(!is_user_logged_in())
        $name = "User not logged in";
    else
         $name=$user->user_firstname.' '.$user->user_lastname; 
    return $name;
}

Hope this helps.

0

No shortcode needed

add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
    foreach ( $menu_items as $menu_item ) {
        if ( strpos($menu_item->title, '#profile_name#') !== false) {
                $menu_item->title =  str_replace("#profile_name#",  wp_get_current_user()->user_firstname, $menu_item->title);
        }
    }

    return $menu_items;
} 

protected by Community Jun 8 '18 at 8:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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