How to use apply_filters() inside a plugin class?

Question

I’d like themes to be able to alter a default array of data inside a plugin class. It’s currently set up like:

namespace Example;

class Example_Class {

    private $stuff;

    public function __construct() {

        $this->stuff = $this->set_stuff();

    }

    public function set_stuff() {

        $things = array(
            'first' => 'First',
            'second' => 'Second',
        );

        return apply_filters( 'my_cool_filter', $things );
     }

}

Then to test it I put this in the theme’s functions.php:

function change_things( $things ) {

    $things = array(
        'third' => 'Third',
        'fourth' => 'Fourth',
    );

    return $things;
 }

 add_filter( 'my_cool_filter', 'change_things' );

However it’s not working. The $stuff property is still getting set as the original default array, so add_filter() isn’t having any effect. It would seem to be the same question as this, but I can’t see what I’m doing wrong.

0
Shoelaced 2 months 0 Answers 13 views 0

Leave an answer