in my functions.php
I have
require 'autoloader.php';
add_action('init', 'MyClass::make');
and in my MyClass
class I have
class MyClass {
public static function make($type) {
var_dump($type);die;
}
}
but the output of var_dump
is string(0) ""
. What I want is to pass the argument to this class as $type
. What I tried was to pass it through do_action
however I think it is not best practice.
How can I pass argument to a method which is inside a class for add_action
?
in my functions.php
I have
require 'autoloader.php';
add_action('init', 'MyClass::make');
and in my MyClass
class I have
class MyClass {
public static function make($type) {
var_dump($type);die;
}
}
but the output of var_dump
is string(0) ""
. What I want is to pass the argument to this class as $type
. What I tried was to pass it through do_action
however I think it is not best practice.
How can I pass argument to a method which is inside a class for add_action
?
2 Answers
Reset to default 1Here's another example on how you could implement the add_action
:
$type = 'my_sample_type';
add_action(
'init',
function() use ( $type ) {
MyClass::make( $type );
}
);
The init
hook doesn't pass any parameters - when you call add_action
, init
isn't passing anything to make
.
You could just call make
outright:
require 'autoloader.php';
MyClass::make( <whatever );
If you need to do this on init
, set up a callback function:
function add_my_type() {
MyClass::make( <foo> );
}
add_action( 'init', 'add_my_type' );