1

While i'm Trying To Develop a plugin, i tried to See some other plugin do the same stuff my plugin will achieve, then i saw The developer in the most functions work with the function apply_filters() and i didn't understand why . This is an Example Below :

public static function myFunction() {
    return apply_filters( 'du/class/method' array(
        'index'    => 'value',
        'index'    => 'value',
        'index'    => 'value',
        'index'    => 'value',
    ) );
}

as you Can See in the first parameter in the function apply_filters() there is a string parameter, everywhere i see this function always i see something like du/class/method .

Please is There Any Explanation For That .

  • 1
    Have you looked at the Codex for how apply_filters works? developer.wordpress.org/reference/functions/apply_filters . Or searched here for many similar questions? Or asked the googles, which might have directed you to other tutorials about this (like wpshout.com/apply_filters-do_action )? – Rick Hellewell May 12 '18 at 2:59
  • @RickHellewell Yes i did all the things you Advices me to do, but unfortunately i didn't understand right what's the point of using it, can you Please Explain to Me Quickly, maybe your answer will be understandable . – Simo Patrek May 12 '18 at 3:32
3

The point is to allow developers to customise aspects of your theme or plugin.

When calling apply_filters() you provide a name and a value that you want developers to be able to filter.

For your example:

$array = apply_filters( 'du/class/method' array(
    'index'    => 'value',
    'index'    => 'value',
    'index'    => 'value',
    'index'    => 'value',
) );

Developers can now change the value of $array in your plugin by using the du/class/method filter with add_filter(). To add an item, perhaps:

function wpse_303378_filter( $array ) {
    $array[] = 'New item';

    return $array;
}
add_filter( 'du/class/method', 'wpse_303378_filter' );

The name of the filter, in this case du/class/method can be whatever you want it to be, but many developers will 'namespace' it so it doesn't conflict with any other plugins. Advanced Custom Fields, for example, prefixes all its hooks with acf/. Your example implies that the filter name uses the class and method that it occurs in as a way to group related filters, but it will depend on the plugin.

You can read more about filters here.

  • I don't Know How to Thank you, now i understand it Very well, so Thank you very Much – Simo Patrek May 24 '18 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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