• I’m new in wordpress development, Can you help me with this question:

    I installed latest wordpress version and latest plugin buddypress version. Now I disabled wordpress admin bar for non admin users and I want move user profile button (screenshot below) into my theme Menu. https://s31.postimg.org/b0s2okcu3/one.png

    My menu: https://s31.postimg.org/5dvpr3abf/two.png

    Now I created new Menu items with login / logout / edit profile functions, but I dont understand how to insert user profile button into my menu.

    There is my code:

    function wpse_125929_login_logout( $items ) {
        if ( is_user_logged_in() ) {
            $current_user = wp_get_current_user();
            $profile_edit_url = admin_url( 'user-edit.php?user_id=' . $current_user->ID );
            $profile_link = '<li><a href="' . $profile_edit_url . '">Edit Profile</a></li>';
            $logout_url = '<li><a href="'. wp_logout_url() .'">Logout</a></li>';
            $items = $items. $profile_link. $logout_url;
        } else {
            $login_link = '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
            $items = $items. $login_link;
        }
        return $items;
    }
    add_filter( 'wp_nav_menu_items', 'wpse_125929_login_logout' );

    How I can add user profile button to my menu?

    Thanks in advance!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You could extend what you already have to add sub-menu items on to the end of the nav menu output, but sub-menu HTML gets rather involved. I think a better way would be to actually add custom links to the profile page, log in page, etc. using the Menu Admin Screen.

    The problem with this interface is everything is static, there is no facility for showing the user name and log out link when logged in, and just Log In if the user is not logged in. What you can do is insert placeholders for certain data when you compose the menu in the Admin Screen. Then using the same ‘wp_nav_menu_items’ filter, search for the placeholders and replace them with appropriate HTML depending on the context.

    For example, add a custom link menu item with a label of “%the_user%” and an URL of http://%user_link%

    In the ‘wp_nav_menu_items’ filter callback, search for “%the_user%” and replace it with the username if logged in and “Log In” if not logged in. Also search for “%user_link%” and replace it with the proper URL depending on what was done with the label, a profile link if logged in, and a log in link if logged out.

Viewing 1 replies (of 1 total)
  • The topic ‘Add user profile button to theme menu’ is closed to new replies.