0

There is a plugin I have installed that loads a metabox of an array ($requirements) list of items in edit post.

When I print_r the plugin's array it shows:

Array
(
    [item1] => Array
        (
            [status] => 1
            [label] => Excerpt has text
            [value] => 1
            [rule] => only_display
            [type] => simple
        )

    [item2] => Array
        (
            [status] => 
            [label] => Between 100 and 600 words
            [value] => Array
                (
                    [0] => 100
                    [1] => 600
                )

            [rule] => block
            [type] => counter
        )

)

I want to change the array and replace with my own array from within the functions.php on editing the post.

The Plugin's Class looks something like:

 class MY_Checklist extends MY_Module
    {

        /**
         * List of requirements, filled with instances of requirement classes.
         * The list is indexed by post types.
         *
         * @var array
         */
        protected $requirements = [];

        /**
         * List of post types which supports checklist
         *
         * @var array
         */
        protected $post_types = [];

        /**
         * Instace for the module
         *
         * @var stdClass
         */
        public $module;

        /**
         * Construct the MY_Checklist class
         */
        public function __construct()
        {
          //.....
        }
        public function display_meta_box($post)
        {
            $requirements = []; //Array I want to change with my own

            $requirements = apply_filters('my_checklist_requirement_list', $requirements, $post) // . <--- Array I want to change with my own
        }


}

This is as far as I've gotten in my function.php and I am not sure what I am doing.

add_action( 'load-edit.php', 'change_checklist_array' );

function change_checklist_array() {

   class MY_NEW_Checklist extends MY_Checklist {

     function display_meta_box( $post ) {  
       $my_new_array = "...";   
     }

   }

}
  • There's a filter right there that you can use: my_checklist_requirement_list. You can read how filters work here: developer.wordpress.org/plugins/hooks/filters – Jacob Peattie Feb 27 at 4:05
  • @JacobPeattie so are you saying I can just do: add_filter(my_checklist_requirement_list, change_checklist_array) within my action or something? – samjco Feb 27 at 4:13
  • Ok, just wanted to know if it is possible to just override the function without using the filter? – samjco Feb 28 at 7:19
  • Depends entirely on the plugin, but based on your example, no. I'm not sure why you'd want to though. – Jacob Peattie Feb 28 at 7:41
  • Well i wondering if It was possible to add an is_page() or is_admin() statement to the function before overriding it's array. I understand I can do it in the add_filter – samjco Feb 28 at 7:54
0

OK, so you’re trying to create your own class that is extending the class from plugin. The problem is that the plugin won’t use your class, so you’d have to modify the plugin.

On the other hand, if you take a closer look at that class:

    public function display_meta_box($post)
    {
        $requirements = []; //Array I want to change with my own

        $requirements = apply_filters('my_checklist_requirement_list', $requirements, $post) // . <--- Array I want to change with my own
    }

you’ll see, that there is my_checklist_requirement_list filter in there and you can use it.

So all you need to do is to put this in your functions.php:

add_filter( 'my_checklist_requirement_list', function( $requirements, $post ) {
    $requirements = array( ... );
    return $requirements;
}, 10, 2 );
  • 1
    Thanks @Krzysiek. Found the issue. It was the priority level add_filter( '...', funct.., 999, 2 ); – samjco Feb 27 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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