Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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'); });
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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