Is a way to pass my own parameters to the function in add_filter
or add_action
.
For example take a look in the following code:
function my_content($content, $my_param)
{
do something...
using $my_param here ...
return $content;
}
add_filter('the_content', 'my_content', 10, 1);
Can I pass my own parameter? something like:
add_filter('the_content', 'my_content($my_param)', 10, 1)
or
add_filter('the_content', 'my_content', 10, 1, $my_param)
By default this is not possible. There are workarounds if you do it the OOP way.
You could create a class to store the values you want to use later.
Example:
Now you can call the class with any function you want â if the function exists somewhere it will be called with your stored parameters.
Letâs create a demo function â¦
⦠and use it once â¦
⦠and again â¦
Output:
The key is reusability: You can reuse the class (and in our examples also the function).
PHP 5.3+
If you can use a PHP version 5.3 or newer closures will make that much easier:
The downside is that you cannot write unit tests for closures.
Use php Anonymous functions:
The correct, really short and most efficient way of passing whatever number of arguments to WP filters and actions is from @Wesam Alalem here, that uses the closure.
I would only add that you could make it even clearer and much more flexible by separating the actual doer method from anonymous closure. For this you just call the method from the closure as follows (modified example from @Wesam Alalem answer).
This way you can write as long or complicated logic as you wish lexically outside of the closure you use to call the actual doer.
Create a function with the needed arguments that returns a function. Pass this function (anonymous function, also known as closure) to the wp hook.
Shown here for an admin notice in wordpress backend.
As mentioned on other answers, passing a parameter to the callback function is not possible by default. OOP and PHP anonymous function are workarounds BUT:
If that is your case, there is another workaround for you to use: make yourself use of the
add_filter
andapply_filters
functions to make that parameter you want to pass available in the callback function:Despite calling a function directly, do this in a more elegant way: pass an anonymous function as a callback.
For example:
I have a single function to translate the title, content, and excerpt from my posts. So, I need to pass to this main function some arguments saying who is calling.
So, the main function
translate_text
receives as many parameters as I want, just because I have passed an anonymous function as a callback.I agree that fuxia’s answer above gives the preferred approaches. But while I was trying to wrap my head around the OOP solution I hit upon a way of doing it that sets and then unsets both the filter and a global variable:
I’m an untrained developer and I work strictly on my own sites, so please take this sketch with a grain of salt. It works for me, but if there’s something wrong with what I’m doing here I’d be grateful if someone more knowledgeable would point it out.
In my OOP solution I simply used a class member variable which is called in the callback function.
In this example the post_title is filtered by a searchterm:
You can always use globals..
if you create your own hook, here is example.
then implement hook:
I know time have passed, but I had some problem with passing my own parameter till I found that the the 4th parameter in add_filter is number of passed parameters including the content to change. So if you pass 1 additional parameter, number should be 2 and not 1 in your case
and using
I was hoping to do the same but since its not possible I guess a simple workaround is to call a different function like
add_filter('the_content', 'my_content_filter', 10, 1);
then my_content_filter() can just call my_content() passing any argument it wants.