0

i want login, logout and view profile functionality. i am currently able to login the user and redirect them to home page of website. i want to show loggedin users name on menu and a logout link. for getting username of currently logged user i tried this:http://codex.wordpress.org/Function_Reference/wp_get_current_user but not able to get the username. for login/logout link i used this code in functions.php

add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
    ob_start();
    wp_loginout('index.php');
    $loginoutlink = ob_get_contents();
    ob_end_clean();
    $items .= '<li>'.  $loginoutlink .'</li>';
    return $items;
 }
2
  • The returned value from wp_get_current_user is an object. How are you retrieving username? Please show us proper code.
    – sri
    Commented Jun 19, 2014 at 10:53
  • I setup whole site on another server and its working there, i think the problem is in server.
    – Kamlesh
    Commented Jun 20, 2014 at 11:45

1 Answer 1

1

I've just done something like this and it's fairly simple

add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
    //check if user is logged in, and add user name to menu 
    if (is_user_logged_in()){
        $user = wp_get_current_user();
        $items .= '<li>' . $user->display_name . '</li>';

    }
    //add login/logout link to menu
    $items .= '<li>'.  wp_loginout(get_permalink(),false) .'</li>';
    return $items;
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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