can I pass arguments to my function through add_ac

2019-01-10 17:16发布

can I do something like that? to pass arguments to my function? I already studied add_action doc but did not figure out how to do it. What the exact syntax to pass two arguments would look like. In particular how to pass text & integer arguments.

function recent_post_by_author($author,$number_of_posts) {
  some commands;
}
add_action('thesis_hook_before_post','recent_post_by_author',10,'author,2')

UPDATE

it seems to me that it is done somehow through do_action but how? :-)

标签: php wordpress
11条回答
小情绪 Triste *
2楼-- · 2019-01-10 18:05

Pass in vars from the local scope FIRST, then pass the fn SECOND:

$fn = function() use($pollId){ 
   echo "<p>NO POLLS FOUND FOR POLL ID $pollId</p>"; 
};
add_action('admin_notices', $fn);
查看更多
Summer. ? 凉城
3楼-- · 2019-01-10 18:11

If you want to pass parameters to the callable function, instead of the do_action, you can call an anonymous function. Example:

// Route Web Requests
add_action('shutdown', function() {
    Router::singleton()->routeRequests('app.php');
});

You see that do_action('shutdown') don't accept any parameters, but routeRequests does.

查看更多
该账号已被封号
4楼-- · 2019-01-10 18:16

Basically the do_action is placed where the action should be executed, and it needs a name plus your custom parameters.

When you come to call the function using add_action, pass the name of your do_action() as your first argument, and the function name as the second. So something like:

function recent_post_by_author($author,$number_of_posts) {
  some commands;
}
add_action('get_the_data','recent_post_by_author',10,'author,2');

This is where it's executed

do_action('get_the_data',$author,$number_of_posts);

Should hopefully work.

查看更多
SAY GOODBYE
5楼-- · 2019-01-10 18:16

Well, this is old, but it has no accepted answer. Reviving so that Google searchers have some hope.

If you have an existing add_action call that doesn't accept arguments like this:

function my_function() {
  echo 100;
}

add_action('wp_footer', 'my_function');

You can pass an argument to that function by using an anonymous function as the callback like this:

function my_function($number) {
  echo $number;
}

$number = 101;
add_action('wp_footer', function() { global $number; my_function($number); });

Depending on your use case, you might need to use different forms of callback, possibly even using properly declared functions, as sometimes you may encounter trouble with scope.

查看更多
家丑人穷心不美
6楼-- · 2019-01-10 18:18

Build custom WP functions with classes

This is easy with classes, as you can set object variables with the constructor, and use them in any class method. So for an example, here's how adding meta boxes could work in classes...

// Array to pass to class
$data = array(
    "meta_id" => "custom_wp_meta",
    "a" => true,
    "b" => true,
    // etc...
);

// Init class
$var = new yourWpClass ($data);

// Class
class yourWpClass {

    // Pass $data var to class
    function __construct($init) {
        $this->box = $init; // Get data in var
        $this->meta_id = $init["meta_id"];
        add_action( 'add_meta_boxes', array(&$this, '_reg_meta') );
    }
    public function _reg_meta() {
        add_meta_box(
            $this->meta_id,
            // etc ....
        );
    }
}

If you consider __construct($arg) the same as function functionname($arg) then you should be able to avoid global variables and pass all the information you need through to any functions in the class object.

These pages seem to be good points of reference when building wordpress meta / plugins ->

查看更多
登录 后发表回答