Edit specific nodes in WP_Admin_Bar

Is it easily possible to edit links in the WP_Admin_Bar global $wp_admin_bar instance?

Related posts

Leave a Reply

2 comments

  1. Yes I recently ran into the situation where I wanted to change the profile link in the user-info section of the admin bar. The problem is that you can only get all nodes, add and remove them. Not edit. And you also cannot modify the $wp_admin_bar->nodes property due it is private.

    When easily removing and adding them, you’ll lose your order and the whole thing looks horrible. Now here is my solution:

    // void jw_edit_admin_bar ( mixed $id , string $property , string $value )
    
    if(!function_exists('jw_edit_admin_bar')) {
        function jw_edit_admin_bar($id, $property, $value) {
            global $wp_admin_bar;
    
            if(!is_array($id)) {
                $id = array($id);
            }
    
            $all_nodes = $wp_admin_bar->get_nodes();
    
            foreach($all_nodes as $key => $val) {
                $current_node = $all_nodes[$key];
                $wp_admin_bar->remove_node($key);
    
                if(in_array($key, $id)) {
                    $current_node->$property = $value;
                }
    
                $wp_admin_bar->add_node($current_node);
            }
        }
    }
    
    add_action('admin_bar_menu', function() { jw_edit_admin_bar(array('user-info', 'my-account'), 'href', 'http://www.nyan.cat'); });
    
  2. Ok, so I recently ran into an issue trying to create a custom Holiday/Time of Day Greeting for the WordPress Admin Toolbar and ran across this answer, which led me down a path of hours of wasted time since it is actually completely unnecessary. You don’t need to destroy and rebuild the admin toolbar to edit a node.

    The simple solution for Julien’s issue is a 5 line addition to your functions.php file that replaces the desired url:

        //-----------------------------------------------------------------------------
    /* change location of user profile page in admin toolbar */
    add_filter( 'edit_profile_url', 'update_admin_bar_user_profile_url', 10, 3 );
    function update_admin_bar_user_profile_url( $url, $user_id, $scheme ) {
        $url = site_url( '/edit-user-profile/' );
        return $url;
    }
    //-----------------------------------------------------------------------------
    

    Now if you want to get a bit deeper into editing/manipulating the the Admin Toolbar – you can do something like this, which adds the website url of the user, if they entered one in their profile page, as a node to the Users Profile Panel (aka a link in the “sub-menu”):

        /* --- add the user website link node in the admin toolbar --- */
    add_action( 'admin_bar_menu', 'update_admin_bar_user_node', 250 );
    function update_admin_bar_user_node( $wp_admin_bar ) {
        $user_id = get_current_user_id();
        $current_user = wp_get_current_user();
        $profile_url = get_edit_profile_url( $user_id );
    
        if ( ! $user_id )
                return;
    
        if ( current_user_can( 'read' ) ) {
            $profile_url = get_edit_profile_url( $user_id );
        } elseif ( is_multisite() ) {
            $profile_url = get_dashboard_url( $user_id, 'profile.php' );
        } else {
            $profile_url = false;
        }
    
        // Add the users website/link to the user-actions sub-menu if they have one 
        $my_account = $wp_admin_bar->get_node( 'my-account' );
        if( ! empty( $current_user->user_url ) && $my_account ){
            $wp_admin_bar->add_node( array(
                'parent'    => 'user-actions',
                'id'        => 'user-url',
                'title'     => '<span class="user-url">' . __( 'My Website' ) . '</span>',
                'href'      => esc_url( $current_user->user_url )
            ) );
        }       
    }
    

    Here is a screenshot of how that change looks in the “my-account” “user-actions” menu:
    enter image description here