4

I'd like to change where the 'Howdy' link gets you when you click on it.

I have a website with buddypress and instead of getting users to their profile page I want to get them to their 'Activity' tab.

How can I change the link?

Thanks, Kat

2 Answers 2

7

It's not well documented, but the add_node and add_menu methods of the WP_Admin_Bar class can be used not only to create new menu or nodes, but also to update an existing menu or node.

So i went ahead and tracked down the code that WordPress initially uses to create that item in the admin bar, replicated it, then made adjustments to the Howdy text and used an example link to google. Simply make your own adjustments as appropriate to the example code.

Example code:
Update the user account menu in the admin bar

function wpse_98066_before_admin_bar_render() {

    global $wp_admin_bar;

    if( !method_exists( $wp_admin_bar, 'add_menu' ) )
        return;

    $user_id      = get_current_user_id();
    $current_user = wp_get_current_user();
    $my_url       = 'http://www.google.com';

    if ( ! $user_id )
        return;

    $avatar = get_avatar( $user_id, 16 );
    $howdy  = sprintf( __('Hey, nice to see you again, %1$s'), $current_user->display_name );
    $class  = empty( $avatar ) ? '' : 'with-avatar';

    $wp_admin_bar->add_menu( array(
        'id'        => 'my-account',
        'parent'    => 'top-secondary',
        'title'     => $howdy . $avatar,
        'href'      => $my_url,
        'meta'      => array(
            'class'     => $class,
            'title'     => __('My Account'),
        ),
    ) );
}
add_action( 'wp_before_admin_bar_render', 'wpse_98066_before_admin_bar_render' );

I hope that helps, have fun. :)

4
  • Thank you for this @t3los! I'm not very familiar with php so I'm sorry if my question is obvious. I assume that I'll have to change the '$profile_url = get_edit_profile_url( $user_id );' and ''href' => $profile_url,' in order to get the link that I want. Am I right?
    – kat_indo
    May 2, 2013 at 13:18
  • @kat_indo - I'll update my answer and give you a direct example, but yes you were indeed correct.
    – t31os
    May 2, 2013 at 13:19
  • I managed to fix it! Yoohoo! That was really helpful! Thank u again!
    – kat_indo
    May 2, 2013 at 13:48
  • Happy i could help. :)
    – t31os
    May 2, 2013 at 13:49
0

Here is a easier and cleaner way...call the node needed and use the part needed and replace what you want to update

function np_replace_howdy($wp_admin_bar){

//New text to replace Howdy
$new_text = 'Welcome';
$my_url       = 'http://www.google.com';

//Call up the 'my-account' menu node for current values.
$my_account = $wp_admin_bar->get_node('my-account');

//Replace the 'Howdy' with new text with string replace
$new_title = str_replace('Howdy', $new_text, $my_account->title);

//Rebuild the menu using the old node values and the new title.
$wp_admin_bar->add_menu(array(
    'id'     => $my_account->id,
    'parent' => $my_account->parent,
    'title'  => $new_title,
    'href'   => $my_url,
    'group'   => $my_account->group,
    'meta'   => array(
        'class' => $my_account->meta['class'],
        'title' => $my_account->meta['title'],
    ),
 ));
}

add_action('admin_bar_menu', 'np_replace_howdy', 999);

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.