Overriding an Array in a Plugin's ClassFunction from functions.php

admin2025-06-02  1

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 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 = "...";   
     }

   }

}
Share Improve this question edited Feb 27, 2019 at 5:37 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 27, 2019 at 3:18 samjco-comsamjco-com 5996 silver badges19 bronze badges 5
  • There's a filter right there that you can use: my_checklist_requirement_list. You can read how filters work here: developer.wordpress/plugins/hooks/filters – Jacob Peattie Commented Feb 27, 2019 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-com Commented Feb 27, 2019 at 4:13
  • Ok, just wanted to know if it is possible to just override the function without using the filter? – samjco-com Commented Feb 28, 2019 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 Commented Feb 28, 2019 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-com Commented Feb 28, 2019 at 7:54
Add a comment  | 

1 Answer 1

Reset to default 1

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 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748861598a314333.html

最新回复(0)