Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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)
share|improve this question

1 Answer 1

up vote 42 down vote accepted

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.

share|improve this answer
5  
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. –  userabuser Nov 17 '13 at 7:17
    
Excellent answer! But how can I do to remove this filter created by this anonymous function later? –  Vinicius Tavares Aug 12 at 3:33
    
@ViniciusTavares You can’t. Think before you use it. :) –  toscho Aug 12 at 8:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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