Passing parameters to filter and action hooks

  1. vaakash


    I think it would be a good feature to pass parameters to functions for which the action and filters are hooked.

    This allows to create some dynamicity while hooking the functions to filters and actions. This can ease some tasks. Not sure why this was added during the WordPress's initial development.

    For example take a look in the following function:

    function my_content($content, $my_param){
    do something...
    using $my_param here ...
    return $content;

    And, add filter/action with parameter like the one below

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

    I asked a discussion community regarding this and I got some OOP related suggestions: http://wordpress.stackexchange.com/questions/45901/passing-a-parameter-to-filter-and-action-functions

    May be this can help and facilitate some new features and plugins in WordPress.

    Thank you,
    Aakash Chakravarthy

    Posted: 4 years ago #
  2. Ipstenu (Mika Epstein)

    You have to remember that WP started 10 (almost 11) years ago as a fork of another project. It wasn't quite possible to do that in the way you're describing, and now we have to maintain back-compat. So there's your "why didn't WP?" answer :) imperfect, but so are we.

    Why doesn't WP now? Backwards compatibility, plus we still support PHP 5.2. I'd expect things like this to be folded in as we move to 5.4 as a base requirement, but we're not there yet.

    Posted: 4 years ago #
  3. Mihail Semjonov

    I think there's a way to use custom parametr..
    But it's per task, i would say..

    So if you need these you'll have to be using at least php 5.3 ( better if 5.4 ).

    so, how i see it:
    Instead of passing functions name ( as seccond argument ) to filter - do the anonimouse function and use keyword use with it..
    so ut will be something like:

    $my_var = 'tratata';
    add_filter('the_content', function( $content ) use($my_var) {
    	return $my_var;

    worked for me

    I think using global would lead to same result.

    global $my_var;
    $my_var = 'tratata';
    add_filter('the_content', 'some_name_fun');
    function some_name_fun( $content ) {
    	global $my_var;
    	return $my_var;

    worked as well

    Posted: 4 years ago #
  4. crgeary


    I've just written an article that covers how you can do this without the use of global variable. Here's the link if you're interested, it covers a few different ways of returning back specific values that you can define at the point of using them.


    To solve your particular case, you can use anonymous functions to end up with something like this:

    function returns($value) {
        return function () use ($value) {
            return $value;
    add_filter('the_content', returns('tratata'));
    Posted: 1 year ago #
  5. lucasbustamante

    You can also do it like this:

    public function __construct() {
        add_action('init', array($this, 'call_somefunction'));
    *    Intermediate function to call add_action with parameters
    public function call_somefunction() {
        $this->somefunction('Hello World');
    *    Actual function that does something
    public function somefunction($text) {
        echo $text;
    Posted: 7 months ago #

RSS feed for this topic


You must log in to post.

  • Rating

    0 Votes
  • Status

    Sorry, not right now