【问题标题】:Where does action or filter parameters come from?动作或过滤器参数从何而来?
【发布时间】:2017-05-06 01:51:51
【问题描述】:

这是 WordPress 中的简单过滤功能。
这段代码的主要课程我已经看懂了,但是有一点不清楚。 我没有在 add_filter 函数中传递 $content 参数,但它是从哪里来的?

如果WordPress支持默认参数,那没关系,那么如何知道特定过滤器或动作事件可能有哪些参数?

<?php
  add_filter( 'the_content', 'prowp_profanity_filter' );
   function prowp_profanity_filter( $content ) {
     $profanities = array( 'sissy', 'dummy' );
     $content = str_ireplace( $profanities, '[censored]', $content );
     return $content;
 }
?>

谢谢。

【问题讨论】:

    标签: wordpress filter parameters hook action


    【解决方案1】:

    the_content 过滤器挂钩位于the_content() 函数内,该函数的代码在wp-includes/post-template.php 核心文件中定义(从第 222 行开始) em>:

    /**
     * Display the post content.
     *
     * @since 0.71
     *
     * @param string $more_link_text Optional. Content for when there is more text.
     * @param bool   $strip_teaser   Optional. Strip teaser content before the more text. Default is false.
     */
    function the_content( $more_link_text = null, $strip_teaser = false) {
        $content = get_the_content( $more_link_text, $strip_teaser );
        /**
         * Filters the post content.
         *
         * @since 0.71
         *
         * @param string $content Content of the current post.
         */
        $content = apply_filters( 'the_content', $content );
        $content = str_replace( ']]>', ']]&gt;', $content );
        echo $content;
    } 
    

    如果您查看代码,您将了解过滤器挂钩中使用的 $content 参数也用作该函数中的变量来操作通过它传递的数据,之前输出它。

    每个动作和过滤器挂钩在核心代码文件或模板中定义了自己的参数,因为它们是一种更改默认行为的方法,而无需更改核心文件或模板的源代码。

    我希望这能回答你的问题。

    同样在互联网上搜索,您会很容易找到所有现有过滤器钩子和动作钩子及其各自参数的列表。

    【讨论】:

      【解决方案2】:

      LoïcTheAztec 是对的,我只是想添加 $content 在函数中触发过滤器时自动填充 (the_content)。

      apply_filters 允许添加额外的参数并将其传递给钩子。你会发现更多细节here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-28
        • 1970-01-01
        • 2020-05-12
        • 2019-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多