2010-08-15 112 views
1

這是我第一次玩過濾器,所以原諒我,如果這似乎基本。我做了一些安靜的搜索,並找不到與我的問題有關的任何事情,從我所能找到的一切應該沒有問題。我的理解是,手動添加的過濾器可以與任何其他過濾器同時運行,wordpress會自動運行,即:創建我自己的過濾器將與wordpress的默認wpautop和wptexturize過濾器一起運行。WordPress的:自定義the_content過濾器是刪除其他過濾器,即:wpautop

但由於某種原因,我添加的過濾器運行良好,除了現在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

你沒有通過已經過篩選,$content到您的過濾功能(因此你失去任何過濾做你達到你的函數之前)。它應該像這樣定義:

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

我明白你的意思了,但我想我仍然缺少一些東西,因爲向函數args中添加$ content是沒有絲毫區別的。是因爲我在函數內從$ post-> post_content提取$內容嗎? – Jervis 2010-08-15 19:39:16

+0

是的,是的。手動拉動post_content也會跳過過濾器修改它的任何操作。很高興知道並感謝您對最初問題的快速回復! – Jervis 2010-08-15 19:59:57