php - Remove items from admin bar - Stack Overflow

admin2025-04-17  2

This placed in functions.php removes the two items from the admin bar. What I would like to know is can they be combined?

$wp_admin_bar->remove_node('imagify'); // This removes the Imagify menu.
$wp_admin_bar->remove_node('maintenance_options'); // This removes the maintenance menu.

Together under the one action?

// remove imagify from admin bar
add_action('wp_before_admin_bar_render', function() {
global $wp_admin_bar;
$wp_admin_bar->remove_node('imagify'); // This removes the Imagify menu.
});

// remove maintenance from admin bar
add_action('wp_before_admin_bar_render', function() {
global $wp_admin_bar;
$wp_admin_bar->remove_node('maintenance_options'); // This removes the maintenance menu.
});

Managed to get the two pieces of code working. Wanting to know if it can be slimmed down.

This placed in functions.php removes the two items from the admin bar. What I would like to know is can they be combined?

$wp_admin_bar->remove_node('imagify'); // This removes the Imagify menu.
$wp_admin_bar->remove_node('maintenance_options'); // This removes the maintenance menu.

Together under the one action?

// remove imagify from admin bar
add_action('wp_before_admin_bar_render', function() {
global $wp_admin_bar;
$wp_admin_bar->remove_node('imagify'); // This removes the Imagify menu.
});

// remove maintenance from admin bar
add_action('wp_before_admin_bar_render', function() {
global $wp_admin_bar;
$wp_admin_bar->remove_node('maintenance_options'); // This removes the maintenance menu.
});

Managed to get the two pieces of code working. Wanting to know if it can be slimmed down.

Share Improve this question asked Feb 1 at 10:42 John WalkerJohn Walker 231 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

You can combine the two wp_admin_bar->remove_node() calls into a single action hook to make the code more concise. Instead of having two separate functions, you can place both remove_node() calls inside one anonymous function.

Here’s how you can combine them:

add_action('wp_before_admin_bar_render', function() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_node('imagify'); // Remove the Imagify menu
    $wp_admin_bar->remove_node('maintenance_options'); // Remove the Maintenance menu
});

Together under the one action?

To "slim it down" directly in functions.php:

$menusToRemove = ['imagify', 'maintenance_options'];

foreach ($menusToRemove as $menu)
    $wp_admin_bar->remove_node($menu);

And also together in one action:

$menusToRemove = ['imagify', 'maintenance_options'];

add_action('wp_before_admin_bar_render', static fn () : void => 
    array_map($wp_admin_bar->remove_node(...)), $menusToRemove);

Or if you prefer curly brackets:

$menusToRemove = ['imagify', 'maintenance_options'];

foreach ($menusToRemove as $menu)
{
    $wp_admin_bar->remove_node($menu);
}

And again in one action:

$menusToRemove = ['imagify', 'maintenance_options'];

add_action(
    'wp_before_admin_bar_render',
    static function () use ($wp_admin_bar, $menusToRemove) : void
    {
        foreach ($menusToRemove as $menu)
        {
            $wp_admin_bar->remove_node($menu);
        }    
    },
);

And with add_action() there is last but not least the option you can make it even more speaking:

$menusToRemove = ['imagify', 'maintenance_options'];

add_action(
    hook:     'admin_bar_menu',
    priority: 500,
    callback: static function (WP_Admin_Bar $wp_admin_bar) use ($menusToRemove) : void
    {
        assert(array_is_list($menusToRemove));
        
        foreach ($menusToRemove as $menu)
        {
            assert(is_string($menu));
        
            $wp_admin_bar->remove_node($menu);
        }    
    },    
);

The admin_bar_menu hook is called right before the wp_before_admin_bar_render hook and gets the WP_Admin_Bar passed as first parameter, therefore, you have it typed already.

It all depends on what you need / prefer.


References:

  • PHP: foreach - Manual (php.net)
  • PHP: Anonymous functions - Manual (php.net)
  • PHP: Arrow Functions - Manual (php.net)
  • PHP: assert - Manual (php.net)
  • add_action() – Function | Developer.WordPress.org (wordpress.org)
  • admin_bar_menu – Hook | Developer.WordPress.org (wordpress.org)
  • wp_before_admin_bar_render – Hook | Developer.WordPress.org (wordpress.org)
  • WP_Admin_Bar::remove_node() – Method | Developer.WordPress.org (wordpress.org)
转载请注明原文地址:http://anycun.com/QandA/1744831575a88230.html