64

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)
1
  • you can use $_SESSION to store and get parameteres. Commented Jul 10, 2020 at 9:23

12 Answers 12

98

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:

/**
 * Stores a value and calls any existing function with this value.
 */
class WPSE_Filter_Storage
{
    /**
     * Filled by __construct(). Used by __call().
     *
     * @type mixed Any type you need.
     */
    private $values;

    /**
     * Stores the values for later use.
     *
     * @param  mixed $values
     */
    public function __construct( $values )
    {
        $this->values = $values;
    }

    /**
     * Catches all function calls except __construct().
     *
     * Be aware: Even if the function is called with just one string as an
     * argument it will be sent as an array.
     *
     * @param  string $callback Function name
     * @param  array  $arguments
     * @return mixed
     * @throws InvalidArgumentException
     */
    public function __call( $callback, $arguments )
    {
        if ( is_callable( $callback ) )
            return call_user_func( $callback, $arguments, $this->values );

        // Wrong function called.
        throw new InvalidArgumentException(
            sprintf( 'File: %1$s<br>Line %2$d<br>Not callable: %3$s',
                __FILE__, __LINE__, print_r( $callback, TRUE )
            )
        );
    }
}

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 …

/**
 * Filter function.
 * @param  array $content
 * @param  array $numbers
 * @return string
 */
function wpse_45901_add_numbers( $args, $numbers )
{
    $content = $args[0];
    return $content . '<p>' . implode( ', ', $numbers ) . '</p>';
}

… and use it once …

add_filter(
    'the_content',
    array (
        new WPSE_Filter_Storage( array ( 1, 3, 5 ) ),
        'wpse_45901_add_numbers'
    )
);

… and again …

add_filter(
    'the_content',
    array (
        new WPSE_Filter_Storage( array ( 2, 4, 6 ) ),
        'wpse_45901_add_numbers'
    )
);

Output:

enter image description here

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:

$param1 = '<p>This works!</p>';
$param2 = 'This works too!';

add_action( 'wp_footer', function() use ( $param1 ) {
        echo $param1;
    }, 11 
);
add_filter( 'the_content', function( $content ) use ( $param2 ) {
        return t5_param_test( $content, $param2 );
    }, 12
);

/**
 * Add a string to post content
 *
 * @param  string $content
 * @param  string $string This is $param2 in our example.
 * @return string
 */
function t5_param_test( $content, $string )
{
    return "$content <p><b>$string</b></p>";
}

The downside is that you cannot write unit tests for closures. 

5
  • 22
    Not only do you get an up-vote for a quality answer to a problem that should have a built in solution within WP core, you also get one for coming back five months later to update your answer with the PHP 5.3+ closure example.
    – Adam
    Commented Nov 17, 2013 at 7:17
  • 1
    Excellent answer! But how can I do to remove this filter created by this anonymous function later? Commented Aug 12, 2014 at 3:33
  • 3
    @ViniciusTavares You can’t. Think before you use it. :)
    – fuxia
    Commented Aug 12, 2014 at 8:17
  • 6
    Note though that if you save the anonymous function to a variable (eg $func = function() use ( $param1 ) { $param1; }; and add_action( $func, 11);) then you can remove it via remove_action( $func, 11 );
    – bonger
    Commented May 2, 2015 at 18:49
  • 3
    But its not advisable to use anonymous functions on plugins or themes you are releasing to the world (you can use them on your own projects). Problem with this is that you won't be able to unhook them. What ever approach you decide to go with should be unhook-able later. Commented Feb 22, 2018 at 16:04
8

Use php Anonymous functions:

$my_param = 'my theme name';
add_filter('the_content', function ($content) use ($my_param) {
    //$my_param is available for you now
    if (is_page()) {
        $content = $my_param . ':<br>' . $content;
    }
    return $content;
}, 10, 1);
2

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.

// ... inside some class

private function myMethod() {
    $my_param = 'my theme name';
    add_filter('the_content', function ($content) use ($my_param) {
        // This is the anonymous closure that allows to pass 
        // whatever number of parameters you want via 'use' keyword.
        // This is just oneliner.
        // $my_param is available for you now via 'use' keyword above
        return $this->doThings($content, $my_param);
    }, 10, 2);
}

private function doThings($content, $my_param) {
    // Call here some other method to do some more things
    // however complicated you want.
    $morethings = '';
    if ($content = 'some more things') {
        $morethings = (new MoreClass())->get();
    }
    return $my_param . ':<br>' . $content . $morethings;
}
1

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.

public function admin_notice_func( $message = '')
{
$class = 'error';
    $output = sprintf('<div class="%s"><p>%s</p></div>',$class, $message);
    $func = function() use($output) { print $output; };
    return $func;
}
$func = admin_notice_func('Message');
add_action('admin_notices', $func);
1

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:

  1. Your code might not be OOP
  2. You might need to remove that filter afterwards

If that is your case, there is another workaround for you to use: make yourself use of the add_filter and apply_filters functions to make that parameter you want to pass available in the callback function:

// Workaround to "save" parameter to be passed to your callback function.
add_filter( 'pass_param', function() use ( $param ){ return $param; } );

// Hook your function to filter whatever you want to filter.
add_filter( 'actual_filter', 'myCallback' );

// Your callback function that actually filters whatever you want to filter.
function myCallback()
{
   // Get the param that we were not able to pass to this callback function.
   $param = apply_filters( 'pass_param', '' );

   // Do whatever with the workarounded-passed param so it can be used to filter.
   return $param;
}
0

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.

add_filter( 'the_title', function( $text ) { 
    return translate_text( $text, 'title', 'pl' );
});

add_filter( 'the_content', function( $text ) { 
    return translate_text( $text, 'content', 'pl' );
});

add_filter( 'the_excerpt', function( $text ) { 
    return translate_text( $text, 'excerpt', 'pl' );
});

So, the main function translate_text receives as many parameters as I want, just because I have passed an anonymous function as a callback.

0

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:

function my_function() {
    
    // Declare the global variable and set it to something
    global $my_global;
    $my_global = 'something';
        
    // Add the filter
    add_filter( 'some_filter', 'my_filter_function' );
    
    // Do whatever it is that you needed the filter for
    echo $filtered_stuff; 
    
    // Remove the filter (So it doesn't mess up something else that executes later)
    remove_filter( 'some_filter', 'my_filter_function' );

    // Unset the global (Because we don't like globals floating around in our code)
    my_unset_function( 'my_global' );
    
}

function my_filter_function( $arg ) {
    
    // Declare the global
    global $my_global

    // Use $my_global to do something with $arg
    $arg = $arg . $my_global;

    return $arg;

}

function my_unset_function( $var_name ) {

    // Declare the global
    $GLOBALS[$var_name];

    // Unset the global
    unset($GLOBALS[$var_name];

}

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.

1
  • Global variables are unprotected: anyone can set them to any value (think naming collisions), and they are very hard to debug. In general, they are seen as bad practice.
    – fuxia
    Commented Jun 22, 2020 at 16:21
0

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:

class MyClass
{
  protected $searchterm = '';

  protected function myFunction()
  {
    query = [
      'numberposts' => -1,
      'post_type' => 'my_custom_posttype',
      'post_status' => 'publish'
    ];

    $this->searchterm = 'xyz';
    add_filter('posts_where', [$this, 'searchtermPostsWhere']);
    $myPosts = get_posts($query);
    remove_filter('posts_where', [$this, 'searchtermPostsWhere']);
  }

  public function searchtermPostsWhere($where)
  {
    $where .= ' AND ' . $GLOBALS['wpdb']->posts . '.post_title LIKE \'%' . esc_sql(like_escape($this->searchterm)) . '%\'';
    return $where;
  }
}
0

You can always use globals..

  global $my_param;
3
  • 1
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – cjbj
    Commented Aug 25, 2017 at 4:25
  • 1
    @cjbj Actually it does. The question is can parameters be passed to the "function" that is in add_filter or add_action. It wasn't clear if the user wanted to pass it in the add_filter or add_action function itself even though that is the assumption. :)
    – samjco-com
    Commented Aug 25, 2017 at 5:17
  • This isn't jeopardy. Answering in the form of a question is more appropriate for comments not answers. I believe that is what cjbj is getting at.
    – Jake
    Commented Dec 9, 2020 at 16:40
-1

if you create your own hook, here is example.

// lets say we have three parameters  [ https://codex.wordpress.org/Function_Reference/add_filter ]
add_filter( 'filter_name', 'my_func', 10, 3 );
my_func( $first, $second, $third ) {
  // code
}

then implement hook:

// [ https://codex.wordpress.org/Function_Reference/apply_filters ]
echo apply_filters( 'filter_name', $first, $second, $third );
2
  • This doesn't pass information from the registration to the callback. It just says how many parameters the callback can accept.
    – fuxia
    Commented May 17, 2015 at 22:15
  • @fuxia, can you suggest a simple change so the information does get passed? Would one just tack on the param values after the 3? Commented May 25, 2019 at 8:32
-1

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

add_filter('the_content', 'my_content', 10, 2, $my_param)

and using

function my_content($content, $my_param) {...}
2
  • 1
    Are you sure you can pass a fifth parameter to add_filter? According to official docs, this is not correct. Have you tested your answer? Please be careful about spreading misinformation.
    – plong0
    Commented Sep 23, 2020 at 19:02
  • This is not how it works, and my testing, so far, confirms that this is not how it works.
    – Jake
    Commented Dec 9, 2020 at 16:52
-2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.