WordPress.org

Ready to get started?Download WordPress

Ideas

Passing parameters to filter and action hooks

  1. vaakash
    Member

    Hello,

    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
    aakashweb.com

    Posted: 4 months ago #
  2. Ipstenu (Mika Epstein)
    Half-Elf Support Rogue & Mod

    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 months ago #
  3. Mihail Semjonov
    Member

    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

    Added:
    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: 3 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    0 Votes
  • Status

    Sorry, not right now