WordPress add_filter( ‘body_class’ ) in __construct not working

It maybe that the filter is firing too late for this to work, but this is what I have, and it isn’t working.

class someClass()
{
    public function __construct()
    {
        add_filter( 'body_class', array( 'someClass', 'body_class_filter') );
    }

    public function body_class_filter( $classes )
    {
        $classes[] = 'some-class';
        return $classes;
    }
}

I have also tried

Read More
add_filter( 'body_class', array( $this, 'body_class_filter') );

or

add_filter( 'body_class', array( &$this, 'body_class_filter') );

and

add_filter( 'body_class', array( __CLASS__, 'body_class_filter') );

But that filter does not appear to be firing. I’ve even put in a trigger_warning() in the function body_class_filter() to see if the code is even processing and it doesn’t appear to be since no warning is showing up in the debug.log.

Ideas?

Related posts

Leave a Reply

1 comment

  1. Are you instantiating the class? Add new someClass(); at the bottom of your file. My suggestion would be to go with the following version of add_filter():

    add_filter( 'body_class', array( $this, 'body_class_filter' ) );
    

    Finally, if you are instantiating the class, are you doing so before the body_class filter is run?