1

I used this function to display a log out link below the home icon. But how could I put a variable, for instance the current username, on that link to show a profile page?

The function is:

    add_action( 'wp_before_admin_bar_render', 'wpdd_admin_bar_edit' );

function wpdd_admin_bar_edit() {
    global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
        'parent' => 'site-name',
        'id' => 'log-out',
        'title' => __( 'Log out' ),
        'href' => 'https://example.com/wp-login.php?action=logout', 
    ));
}

1 Answer 1

0

You can use wp_get_current_user() function to retrieve user login data. Also use get_edit_profile_url() to get profile page link.

Note: You can use wp_logout_url() function for logout url.

Try following code:

add_action( 'wp_before_admin_bar_render', 'wpdd_admin_bar_edit' );

function wpdd_admin_bar_edit() {
    global $wp_admin_bar;

    // Show username with profile link 
    $current_user = wp_get_current_user();
    $username = $current_user->user_login;
    $profile_link = get_edit_profile_url();
    $wp_admin_bar->add_menu( array(
        'parent' => 'site-name',
        'id' => 'my_profile',
        'title' => __( 'Profile: ' ) . $username,
        'href' => $profile_link
    ));

    // Logout Link
    $logout_url = wp_logout_url();
    $wp_admin_bar->add_menu( array(
        'parent' => 'site-name',
        'id' => 'log-out',
        'title' => __( 'Log out' ),
        'href' => $logout_url
    ));
}
2
  • Cool! Thank you!
    – markseinto
    Dec 27, 2022 at 15:03
  • @markseinto . If this answers your question, please consider marking it as the correct answer. It will helps others find it as resolved.
    – Irfan
    Dec 29, 2022 at 7:54

Your Answer

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

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