doumin1897 2010-08-15 19:20
浏览 117
已采纳

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

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 2010-08-15 19:26
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献