show_admin_bar filter not called inside plugin class

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

tech