WordPress.org

Ready to get started?Download WordPress

Forums

add_action & function values (3 posts)

  1. TrevorNet
    Member
    Posted 3 years ago #

    I know this has been covered time and again, but most of the threads are closed or come close to my question without a definitive answer.

    I understand add_action. At least the first 3 params of the add_action call itself. What I am not understanding is how the 4th param translates into actual values passed to the named function. Say I have a function called liquid_rain that takes 3 parameters.

    function liquid_rain($param1, $param2, $param3) {
     // Do something with the params.
    }

    From a random plug-in or my theme functions.php file I have the following line:
    add_action('save_post','liquid_rain', 8, 3)

    How does add_action(x,x,x,3) translate into the actual param values for liquid_rain? Simply calling the function through add_action does not (it appears) assign the required values. Further more, what if the values for liquid_rain come from a source other than a form. Say - as a reaction to a conditional.

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    The fourth parameter simply tells WordPress how many args the callback function can accept.

    The action or filter you're hooking onto ultimately decides how many variables it will pass to your action or filter callback function.

    Imagine for a second this is your callback function.

    function my_func( $one, $two, $three, $four ) {
       // Doing something
    }

    And now imagine you're going to attach that function to a WP filter, and also let's say for a moment this is how the code looks in WordPress.

    // Code sample 1
    $a = 1;
    $b = 2;
    apply_filters( 'my_filter_hook', $a, $b );

    Now let's say we attach the function onto that hook and tell WP we can support 4 arguments, eg..

    add_filter( 'my_filter_hook', 'my_func', 10, 4 );

    Since the hook has only provided two variables, the callback function will only receive data in the first two args, ie. $one and $two..

    If instead the imaginary core code looked like this..

    // Code sample 2
    $a = 1;
    $b = 2;
    $c = 3;
    $d = 4;
    apply_filters( 'my_filter_hook', $a, $b, $c, $d );

    We'd then find the hook has passed on data to all four of the callback functions args $one, $two, $three and $four.

    I'm not sure how else to explain it without making it more confusing, so i hope that helps.. ;)

  3. TrevorNet
    Member
    Posted 3 years ago #

    Ha! I am already a bit confused, but your response helps clear things up. Finding the relationship between the various parts of WordPress has been very interesting to say the least. Thank You!

Topic Closed

This topic has been closed to new replies.

About this Topic