admin管理员组文章数量:1434909
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' );
本文标签: phpHow to pass argument to addaction while the method is inside a class
版权声明:本文标题:php - How to pass argument to add_action while the method is inside a class? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745636331a2667581.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论