Define a function outside a class and call the function using action or filter hook

Hi Currently i am developing a sub plugin for my own main plugin. Please see the main plugin code below

<?php
class MyClass
{
    function __construct()
    {

        function test1()
        {
            echo 'hii- test1';
        }
        $this->autoload_function();
    }

    function autoload_function()
    {
        $my_array = array(
            "test1"
        );

        foreach ($my_array as $my_function)
        {
            call_user_func($my_function);
        }

    }

}

$var = new MyClass();

?>

My sub plugin need to add more values to $my_array and i am allowed to use do_action or apply_filtere in main plugin.

I added apply_filters, so that i can modify the $my_array in my sub plugin.

my-class.php

<?php
class MyClass
{
    function __construct()
    {

        function test1()
        {
            echo 'hii- test1';
        }
        $this->autoload_function();
    }

    function autoload_function()
    {
        $my_array = array(
            "test1"
        );

        apply_filters('modify_array', $my_array);
        foreach ($my_array as $my_function)
        {
            call_user_func($my_function);
        }

    }

}

$var = new MyClass();

?>

In my sub plugin i checked the filter and i can see that filter is working perfectly so that i can modify the value of $my_array in my sub plugin.

my-newpage.php

add_filter ('modify_array', 'modify_array_function', 0);


function modify_array_function($my_array){
 array_push($my_array, 'test2')
}

Here i can see that new value is added to array but now i have to define test2 function inside my sub plugin .

function test2(){
 echo 'hii- test2';
}

When i write test2 function in my sub plugin i am getting below error .

Warning: call_user_func() expects parameter 1 to be a valid callback,
class ‘MyClass’ does not have a method
‘test2’

Now how can i solve this ? Do i need to add more action or filters in the sub plugin .

The issue is due to call_user_func('test2'); is called inside my-class.php but the test2 function is defined outside mycalss. It is defined inside my sub plugin.

Please help to solve this error.

1 Answer
1

So I presumed your sub plugin loads after the main one, right?

Here i can see that new value is added to array but now i have to define test2 function inside my sub plugin

If you’re certain test2 successfully added to the array and that test2 was being called from MyClass::autoload_function(), then you can try using an early hook like init to define test2, like so:

<?php
/* Plugin Name: My Sub Plugin */

// Defines the test2() function.
function my_define_test2() {
    function test2(){
        echo 'hii- test2';
    }
}

// Now hook on init to ensure test2() is defined before your main plugin
// is loaded.
add_action( 'init', 'my_define_test2', 1 );

// And then add test2() to $my_array (that you defined in the main plugin).
add_filter( 'modify_array', 'modify_array_function' );
function modify_array_function( $my_array ) {
    array_push( $my_array, 'test2' );
    return $my_array; // remember, filters must always return something =)
}

If however MyClass is instantiated immediately when WordPress loads the plugin, i.e. something like so, then I don’t think it’s possible for you to define test2 before MyClass is instantiated. Unless maybe, if you use a Must Use plugin.

<?php
/* Plugin Name: My Main Plugin */

class MyClass { ... }

$var = new MyClass();

Leave a Comment