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? 🙂
Yes you can! The trick really is in what type of function you pass to add_action and what you expect from do_action.
We can do it with a closure.
Here is a simplified example of a closure working
Anonymous vs. Closure
Proxy Function Class
Example Usage #1
Example Usage #2
Instead of:
it should be:
…where 2 is the number of arguments and 10 is the priority in which the function will be executed. You don’t list your arguments in add_action. This initially tripped me up. Your function then looks like this:
Both the add_action and function go in functions.php and you specify your arguments in the template file (page.php for example) with do_action like so:
Hope this helps.
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 ->
7 Ways to pass data to
add_action
functiondo_action
(if you yourself create actions)wp_localize_script
approach (if you need to pass data to JavaScript)use
in Closures/Anonymous/Lamda functionsadd_filter
,apply_filters
as a transport (clever way)global
or$GLOBALS
(if you are desperate)set_transient
,get_transient
and other functions as a transport (in case of exotic necesities)#1 Through
do_action
if you have access to the code where the action fires, pass variables through
do_action
:#2
wp_localize_script
approachif you need to pass variable to JavaScript, this is the best way of doing it.
functions.php
my-script.js
Basically the same but without
wp_localize_script
:functions.php
#3 Using
use
in Closures/Anonymous/Lamda functionsif you don’t have access to the code where the action fires you can slip the data as follows (PHP 5.3+):
#4 Make use of arrow functions (PHP 7.4+)
Basically the same as the #3 example but more concise, as the arrow functions involve variables from the parent scope without using
use
:#5 Use
add_filter
,apply_filters
as a transportYou can create a function with
add_filter
that will return value when you callapply_filters
:I have seen the approach applied in many plugins.
#6 Hack the scope with
global
or$GLOBALS
(kiddy way)If you don’t worry about the scope, use
global
, example #1:Example #2 Using
$GLOBALS
instead ofglobal
#7 Use
set_transient
,get_transient
,set_query_var
,get_query_var
as a transportExample #1: Let’s say there is a shortcode that prints a form, that subsequently gets submitted and handled via AJAX, and the data is coming from the form must be sent by email that should be gotten from the shortcode parameters.
— Within Ajax handler —
Example #2: Before WordPress 5.5 came out, some people had passed parameters within
wp_query
byget/set_query_vars
to pass them to the template parts, these can be used as well.Mix them up and use. Cheers.
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.
Pass in vars from the local scope FIRST, then pass the
fn
SECOND:I use closure for PHP 5.3+. I can then pass the default values and mine without globals. (example for add_filter)
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.
I ran into the same thing today, and since all answers here are either unclear, irrelevant or excessive, I thought I’d provide the simple straight-forward answer.
Like the most popular answer here already states, you should use an anonymous function to achieve what you’d like to do. But, what deserves some extra attention IMO, is the the benefit of passing the available parameters of the action to your function.
If somewhere, an action hook is defined like this:
You can pass the values of
$first_param
and$second_param
to your own function, and add your own params like this:Then you can use all the values in your method, like this:
I’ve wrote wordpress plugin long time ago, but I went to WordPress Codex and I think that’s possible: http://codex.wordpress.org/Function_Reference/add_action
I think you should pass them as an array. Look under examples “take arguments”.
Bye
I ran into the same issue and solved it by using global variables. Like so:
A bit sloppy but it works.
Why not simply as this:
Just wanted to add this here so that when I search for this next year, I know where to find this. This is modified from the latest WordPress documentation.
Here is how to do it from WordPress’s documentation.
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.Do
then
More info https://tommcfarlin.com/wp_redirect-headers-already-sent/
I have made a code to send parameters and process.