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