phpwordpressitems

Remove items from admin bar


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.


Solution

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