【问题标题】:Do I have to add_filter() before apply_filters() in Wordpress?我必须在 Wordpress 中的 apply_filters() 之前 add_filter() 吗?
【发布时间】:2011-03-23 06:00:21
【问题描述】:

我正在尝试理解 Wordpress 插件,例如:

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

我正在查找 Wordpress 中的所有代码,但找不到:

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

为什么这个插件没有add_filter?或者我错过了什么?类似的东西:

do_action('wp_loaded');

我找不到:

add_action('wp_loaded', ....);

【问题讨论】:

    标签: wordpress plugins filtering


    【解决方案1】:

    apply_filters 就像,'如果有任何具有此名称的过滤器,请使用这些参数运行附加的回调'。因此,如果该名称没有add_filter,则意味着目前没有将与apply_filters 调用一起运行的过滤器。

    do_actionadd_action 也是如此。

    【讨论】:

    • 感谢回复。我在想它应该像你说的那样。但是,我对 WordPress 感到困惑,它一直在使用 apply_filters。就像在 class-http.php 中一样,有: return apply_filters('use_http_extension_transport', function_exists('http_request'), $args );为什么他们只是返回 function_exists('http_request') ?
    • if there are any filters with this name, run the attached callbacks with these parameters - 它在 wordpress 文档网站上的什么地方说?
    【解决方案2】:

    我在插件或主题中遇到过这种类型的代码,其中使用了apply_filter,但不一定有现有的filteradd_filter

    在这种情况下,如果使用 apply_filters 而不使用过滤器,则您必须在要运行它的地方再次调用该函数。例如,在主题的标题中。

    以下是在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 
        }
    }
    

    现在在header.php 文件中,你必须调用这个函数,因为它没有被挂在任何地方。因此,在这种情况下,要在标题中显示输出,您可以像这样调用函数:

    <?php  header_apply_filter_test(); ?>
    

    您也可以使用钩子编写此代码,它会做同样的事情,即在标题中显示输出。

    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; 
            }
        } 
    

    对于第二个选项,您仍然可以在其他任何地方使用 apply_filters 来调用回调函数 header_apply_filter_test(),因为过滤器现在存在。

    所以在我看来,底线是一个用例,因为任何一种方法都有效!

    【讨论】:

      【解决方案3】:

      我也是 PHP - WordPress 堆栈的初学者,但这是我的理解。

      插件调用apply_filters 代码中没有任何add_filter允许网站用户向他们的插件添加自定义逻辑。我们 - 用户,可以添加我们自己的功能并使用add_filter注册我们的功能。

      例如,这段代码来自插件。通常,它会显示所有产品,但它为我们提供了一种不显示特定产品的方法。

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

      所以,如果我们 - 用户,想要定制一点。我们可以添加我们自己的函数如下(可能在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 );
      

      这意味着:插件会正常运行,但对于我自己的网站,它会注明显示 ID 为 5 的产品!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-08
        • 2021-10-01
        • 1970-01-01
        • 2014-06-04
        • 2013-06-10
        • 2011-01-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多