doumin1897
doumin1897
2010-08-15 19:20

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

  • hook
  • filter
  • php
  • wordpress
已采纳

This is my first time playing with filters, so forgive me if this seems basic. I have done quiet a bit of googling and can't find anything related to my problem, from everything I have been able to find this should be working without an issue. It's my understanding that filters added manually run concurrent with any other filters wordpress is automatically running, ie: creating my own filter will run along with wordpress's default wpautop and wptexturize filters.

But for some reason my added filter is running fine, except now wpautop and wptexturize is failing to run on the_content. After removing my custom filter from functions, the default filters are triggering again. No other plugins installed that are modifying the output. Any help would be super appreciated, along with any general recommendations as to a better way to do this if you see something wrong with my statements.

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\">
";
            $split = preg_split("/(?
)+|(<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>
";
                    }
                    else {
                    // the string didn't contain a needle so we will keep it and output it later
                        $no_chatoutput = $no_chatoutput . $haystack . "
";
                    }
                }
                // print our new formated chat post and push unformatted content to the end of the post.
                $content = $chatoutput . "</dl>
" . $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条回答

  • dongyi1015 dongyi1015 11年前

    You're not passing the already filtered $content into your filter function (and therefore you're losing any filtering done before you reach your function). It should be defined like so:

    function tumblr_chat_post($content) {
       // rest of your logic m'ere
    }
    
    点赞 评论 复制链接分享