• Resolved loopforever

    (@loopforever)


    Hi,
    I want to ask a point that I don’t understand.
    (https://developer.www.ads-software.com/reference/functions/apply_filters/#parameters)

    1-) The $ args parameter is called “mandatory”, but it may not be defined in the examples. So, $ tag and $ value should definitely be found. Therefore, 2 parameter apply_filters can be defined. Did I get it wrong? Isn’t the undefined value in such a scenario $ args?

    2-) Can $ arg1 and $ arg2 be changed in the example_callback function in a definition like the one below? If no, are these arguments ($ args1 and $ args2) just operable arguments? Just like actions?

    // The filter callback function.
    function example_callback( $string, $arg1, $arg2 ) {
        // (maybe) modify $string.
        return $string;
    }
    add_filter( 'example_filter', 'example_callback', 10, 3 );
    
    /*
     * Apply the filters by calling the 'example_callback()' function
     * that's hooked onto <code>example_filter</code> above.
     *
     * - 'example_filter' is the filter hook.
     * - 'filter me' is the value being filtered.
     * - $arg1 and $arg2 are the additional arguments passed to the callback.
    $value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
    
    ?>

    For example:

    function example_callback( $string, $arg1, $arg2 ) {
        $args1 ['hierarchical'] = true;
        return $args;
    }

    Is it right ?

    • This topic was modified 4 years, 1 month ago by loopforever.
    • This topic was modified 4 years, 1 month ago by loopforever.
Viewing 3 replies - 1 through 3 (of 3 total)
  • There is a problem with the parameter description on that page. I have asked one of the docs team to take a look.
    The $args parameter is erroneously listed and marked as required.

    Thread Starter loopforever

    (@loopforever)

    I understand. Well, how should it be?
    Also question 2.
    Thank you.

    • This reply was modified 4 years, 1 month ago by loopforever.

    I don’t understand the question. Are you trying to get a change made to the doc page, or just trying to understand how filters work?
    Someone will be looking at that page now, he said he’d look after the release of 5.6.1, which just happened.

    To understand filters, keep in mind that only one variable is being filtered. By this I mean that apply_filters() function loops through all the filters(functions) that have been added(using add_filter()) for the $tag, passing the return value of the first into the second and so on. There can be additional parameters passed, but they remain the same for each call. So a filter function can only modify its first parameter, and it must return something or nothing will be passed to the next filter in the list. The additional parameters are for context only.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘apply_filters’ is closed to new replies.