Change username link in admin bar

Luca Bellipanni asked
php links buddypress
via

when i click on the username in the adminbar it redirect me to the edit profile page of buddypress. My question is,how can i change it to user activity page? thank you very much.


Answer
via

Create the following directory and file.
wp-content/mu-plugins/admin-bar-tweaks.php
Or, you can add this to your theme’s functions.php file.

  • In the script below, remember to set $new_url to whatever you desire.

<?php // Requires PHP 5.4+.
add_action( 'admin_bar_menu', function( $wp_admin_bar ) {
    $new_url = 'http://example.com/custom/location';

    if ( $wp_admin_bar->get_node( 'my-account') ) {
        $wp_admin_bar->add_node( [
            'id'   => 'my-account',
            'href' => $new_url,
        ] );
    }
    if ( $wp_admin_bar->get_node( 'user-info') ) {
        $wp_admin_bar->add_node( [
            'id'   => 'user-info',
            'href' => $new_url,
        ] );
    }
    if ( $wp_admin_bar->get_node( 'edit-profile') ) {
        $wp_admin_bar->add_node( [
            'id'   => 'edit-profile',
            'href' => $new_url,
        ] );
    }
} );

A quick walk through this.

  • Attach to the admin_bar_menu hook.
  • Check if the my-account, user-info, and/or edit-profile nodes exist.
  • If each/any of them do, add them back in again, this time with a new URL. Note: When calling WP_Admin_Bar::add_node(), any properties you do not define will simply be set to whatever they were already. In this case, we are simply changing the href to $new_url.

To learn more, see: WP_Admin_Bar
See also: WP_Admin_Bar::add_node()
See also: default nodes in WordPress core.

Share This
Posted in: