Edit specific nodes in WP_Admin_Bar

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'); });

Leave a Comment