I have this code inside my plugin class, what I want to achive is to hide the admin bar to non admin users and custom roles of my wp setup

    function hide_admin_bar()
        if( !is_user_admin() && !current_user_can( 'manage_options' ) ){
            return false;

And into the constructI'm calling the needed filter

add_filter( 'show_admin_bar', array( $this, 'hide_admin_bar' ) );

I'm not sure why but I've noticed that the filter isn't called, is there any reason because this can occur and how fix it?

  • How are you instantiating the class? Also, your filter should pass through the original value of your condition is not met, otherwise you're just hiding the admin bar for everyone all the time, regardless of their role. Commented Sep 11, 2023 at 11:59
  • @JacobPeattie the class is normally called using new MyClassName(); inside the plugin file. Into the example of code there is a typo, I need to hide the bar only forn non admin.
    – OHICT
    Commented Sep 11, 2023 at 13:54

1 Answer 1


As Jacob Peattie mentioned in the comments, the filter should return a value, if the condition is not met, as demonstrated below.

Another thing to consider is the question of when are you instantiating the class? I.e. immediately on the main plugin file or on some action hook. Although I doubt that this is the case here, it is something to remember when working with WordPress that things happen in a certain order and if you attach filters or actions too late, they won't get executed as the thing you were trying to affect has already happened.

Perhaps the more interesting question is, why are you using is_user_admin()? Do you actually want to know, if the "current request is for a user admin screen" and "if inside WordPress user administration pages" as mentioned in the documentation?

If you're looking for a way to check for a certain user role, then have a look at this Q&A: How to check if a user is in a specific role?.

And as a side note regarding your mention, "And into the construct I'm calling the needed filter". If you want to have more separation in your code, pull the filters, and actions if any, out of the constructor and attach the object to them outside of the class. I.e.

class MyClassName
    public function __construct(array $config)
        // do something with $config

    public function hide_admin_bar(bool $show_admin_bar): bool
        // conditional checks...

        return ! $show_admin_bar;

add_action('plugins_loaded', 'wpse418622_init_my_class');
function wpse418622_init_my_class(): void {
    $maybe_some_configuration_data = [];

    $my_class = new MyClassName($maybe_some_configuration_data);

    add_filter( 'show_admin_bar', array( $my_class, 'hide_admin_bar' ) );
  • So I'm calling the filter too late to get the result I want? I can remove the is_user_admin function and leave the current_user_can function but need to understand as you've pointed, what's the best way to accomplish my task and hide the bar.
    – OHICT
    Commented Sep 12, 2023 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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