开发者

Do I have to add_filter() before apply_filters() in Wordpress?

开发者 https://www.devze.com 2023-02-18 22:49 出处:网络
I\'m trying to understand Wordpress plugin like: apply_filters( \'gettext\', $translations->translate( $text ), $text, $domain );

I'm trying to understand Wordpress plugin like:

apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );

I'm looking for all codes in Wordpress, I can't find:

add_filter( 'gettext', ....);

Why there is no add_filter for this plugin? Or I missed something? Same thing like:

do_action('wp_loaded');

I can't find:

a开发者_C百科dd_action('wp_loaded', ....);


apply_filters is like, 'if there are any filters with this name, run the attached callbacks with these parameters'. So if there is no add_filter for that name, it means that there is no filter that's going to be run with the apply_filters call at the moment.

The same goes with do_action and add_action.


I am a beginner in PHP - WordPress stack as well, but this is from my understanding.

The plugins call apply_filters without having any add_filter in their codes is to allow the website users to add custom logic to their plugins. We - the users, can add our own function and use add_filter to register our functions.

For example, this piece of code is from the plugin. Normally, it shows all products but it provides us a way to not show a specific product.

// Plugin's

if (apply_filters( 'plugin_show_products', true, $product->get_id() ) ) {
    $this->show_products();
}

So, if we - the users, want to customize a bit. We can add our own function as following (maybe in functions.php)

// Our custom changes
function my_own_changes($boolean, $product_id) {
    if ( $product_id === 5 ) return false;
    return true;
}
add_filter( 'plugin_show_products', 'my_own_changes', 10, 2 );

This translates to: The plugin will behave normally but for my own site, it will not show the product with ID of 5!


I have come across this type of code in a plugin or theme where the apply_filter is used without necessarily having an existing filter or add_filter

In this case, where the apply_filters is used without a filter you will have to call the function again where you want to run it. For example, in the header of a theme.

The following is an example of apply filters used in a function that is again called in the header.php

if ( ! function_exists( 'header_apply_filter_test' ) ) {

    function header_apply_filter_test() {

        $filter_this_content = "Example of content to filter";

        ob_start();

            echo $filter_this_content; 

        $output = ob_get_clean();

        echo apply_filters( 'header_apply_filter_test', $output );//used here 
    }
}

Now in the header.php file, you would have to call this function since it is not hooked anywhere. So, in this case, to display the output in the header you would call the function like this :

<?php  header_apply_filter_test(); ?>

You could as well write this code with a hook and it would do the same thing i.e display the output in the header.

add_filter('wp_head', 'header_apply_filter_test');

if ( ! function_exists( 'header_apply_filter_test' ) ) {

        function header_apply_filter_test() {

            $filter_this_content = "Example of content to filter";

            ob_start();

                echo $filter_this_content; 

            $output = ob_get_clean();

            echo $output; 
        }
    } 

For this second option, you would still have the capability of using apply_filters anywhere else to call the callback function header_apply_filter_test() since the filter now exists.

So the bottom line in my view is a use case since either approach works!

0

精彩评论

暂无评论...
验证码 换一张
取 消