2

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.

5
  • Is test2 a member function of your class? Can't you just make it a user defined function, and call it from anywhere then?
    – Howard E
    Jan 1 at 17:18
  • Are you sure the main method is working? It should fail with "expects parameter 1 to be a valid callback, function 'test1' not found" already, see this online example.
    – kero
    Jan 4 at 11:34
  • @kero thanks for checking. I have modified my code. please see this 3v4l.org/eBnNH Jan 5 at 3:45
  • @SallyCJ While I like the metaphor, it certainly isn't true for PHP.
    – kero
    Jan 7 at 18:03
  • @kero thanks, and yes, because your foo() function was defined in the same scope, and that's what I meant by "be defined before being called".. Anyway, I think I have misunderstood OP, hence I deleted all my previous comments. And I've posted an answer, so if you noticed anything (wrong), let me know..
    – Sally CJ
    Jan 9 at 1:13
3
+50

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();

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.