Wordpress:自定义the_content过滤器正在删除其他过滤器,即:wpautop

时间:2010-08-15 19:20:53

标签: php wordpress filter hook

这是我第一次玩过滤器,如果这看起来很基本,请原谅我。我已经做了一些谷歌搜索安静,无法找到任何与我的问题相关的东西,从我能够找到的所有内容应该没有问题。我的理解是手动运行的过滤器与任何其他过滤器并行运行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);

1 个答案:

答案 0 :(得分:1)

您没有将已经过滤的$content传递给您的过滤器功能(因此您在到达功能之前就失去了任何过滤功能)。它应该这样定义:

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