【问题标题】:Wordpress: custom the_content filter is removing other filters, ie: wpautopWordpress:自定义 the_content 过滤器正在删除其他过滤器,即:wpautop
【发布时间】:2011-03-30 03:58:58
【问题描述】:

这是我第一次使用过滤器,如果这看起来很简单,请原谅我。我已经安静地进行了一些谷歌搜索,但找不到与我的问题相关的任何内容,从我所能找到的一切来看,这应该可以正常工作。据我了解,手动添加的过滤器与 wordpress 自动运行的任何其他过滤器同时运行,即:创建我自己的过滤器将与 wordpress 的默认 wpautop 和 wptexturize 过滤器一起运行。

但由于某种原因,我添加的过滤器运行良好,除了现在 wpautop 和 wptexturize 无法在 the_content 上运行。从函数中删除我的自定义过滤器后,默认过滤器再次触发。没有安装其他修改输出的插件。如果您发现我的陈述有问题,我们将非常感谢您提供任何帮助以及任何关于更好方法的一般性建议。

function tumblr_chat_post($content) {
global $post;
$content = $post->post_content;
if ($post->post_type == 'post') {
    $postcats = wp_get_object_terms($post->ID, 'category');
    foreach ($postcats as $mycat) {
        if ($mycat->name == "chats") {
            $chatoutput = "<dl class=\"transcript\">\n";
            $split = preg_split("/(\r?\n)+|(<br\s*\/?>\s*)+/", $content);
                foreach($split as $haystack) { 
                    if (strpos($haystack, ":")) {
                        $string = explode(":", trim($haystack), 2);
                        //list($who, $what) = explode(":", $haystack, 2);
                        $who = trim($string[0]);
                        $what = trim($string[1]);
                        $row_class = empty($row_class)? " class=\"alt\"" : "";
                        $chatoutput = $chatoutput . "<dt$row_class><b>$who:</b></dt> <dd><i>$what</i></dd>\n";
                    }
                    else {
                    // the string didn't contain a needle so we will keep it and output it later
                        $no_chatoutput = $no_chatoutput . $haystack . "\n";
                    }
                }
                // print our new formated chat post and push unformatted content to the end of the post.
                $content = $chatoutput . "</dl>\n" . $no_chatoutput;
                return $content;
        } 
        else {
        // I don't exist in the defined category, so no processing is needed
        return $content;
        }
    }
} 
else { 
    // I'm not a regular post, so no processing is needed.
    return $content;
}
}
add_filter( "the_content", "tumblr_chat_post", 20);

【问题讨论】:

    标签: php wordpress filter hook


    【解决方案1】:

    您没有将已过滤的$content 传递到您的过滤器函数中(因此您将丢失在到达函数之前完成的任何过滤器)。应该这样定义:

    function tumblr_chat_post($content) {
       // rest of your logic m'ere
    }
    

    【讨论】:

    • 我明白你的意思,但我想我仍然缺少一些东西,因为在函数 args 中添加 $content 并没有产生丝毫区别。是因为我在函数中从 $post->post_content 中提取 $content 吗?
    • 是的,是的。手动拉出 post_content 也会跳过过滤器为修改它所做的任何事情。很高兴知道并感谢您对最初问题的快速回复!
    猜你喜欢
    • 2013-01-19
    • 2012-11-28
    • 2012-01-22
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 2014-11-19
    相关资源
    最近更新 更多