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? :-)
Pass in vars from the local scope FIRST, then pass the
fn
SECOND:If you want to pass parameters to the callable function, instead of the do_action, you can call an anonymous function. Example:
You see that
do_action('shutdown')
don't accept any parameters, butrouteRequests
does.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:This is where it's executed
Should hopefully work.
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:You can pass an argument to that function by using an anonymous function as the callback like this:
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.
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...
If you consider
__construct($arg)
the same asfunction 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 ->