2013-07-19 126 views
0

我想the_content駐留在/wp-includes/post-template.php從在我的插件我想改變the_content

function the_content($more_link_text = null, $stripteaser = false) { 
    $content = get_the_content($more_link_text, $stripteaser); 
    $content = apply_filters('the_content', $content); 
    $content = str_replace(']]>', ']]>', $content); 
    echo $content; 
} 

變成

function the_content($more_link_text = null, $stripteaser = false) { 
    $content = get_the_content($more_link_text, $stripteaser); 
    $content = apply_filters('the_content', $content); 
    echo $content; 
} 

怎麼辦功能我在我的插件中完成了此操作,但未觸及wordpress代碼(讓我的代碼升級兼容)?我知道某些功能可以被替換,但是這個呢?

+0

我很好奇,是str_replace函數你一個問題嗎?我試圖記住它是什麼... –

+0

是的。我在wp_head中注入了一個google跟蹤標記,並且它包含一個由str_replace函數取代的CDATA結束標記。 – AleV

+0

CDATA和'wp_head'細節應該在問題中。 ''與'the_content'無關,是嗎? – brasofilo

回答

0

WordPress的核心功能列表可以覆蓋主題和插件;他們被稱爲可插拔功能: http://codex.wordpress.org/Pluggable_Functions

the_content()不在該列表中,所以它不能直接替換。

如果你不想編輯WordPress代碼,那麼沒有簡單的方法來完成它。唯一的選擇是創建函數的本地副本(稱爲myTheme_the_content(),或類似的東西),並確保您改變主題中的引用來調用它。

0

答案是在WordPress代碼本身:$content = apply_filters('the_content', $content);。見Actions and filters are NOT the same thing…

你可以連接到該過濾器和修改的內容:

<?php 
/* Plugin Name: Modify Content */ 

add_filter('the_content', 'mod_content_so_17749899'); 
function mod_content_so_17749899($content) 
{ 
    // http://codex.wordpress.org/Conditional_Tags 
    if(is_admin()) // Prevent this hook in admin area 
     return $content; 

    // Manipulate $content 
    return $content; 
} 
+0

您的代碼不起作用,因爲主函數中的str_replace過濾器在您的建議過濾器之後應用**。這就是爲什麼我需要更換功能。 – AleV

+0

@AleV,一定要拍。我有這種感覺,我忽略了一些東西。 – brasofilo

+0

另外我發現這個added_filter在我的wp_head或woocommerce_thankyou函數中沒有被考慮(但是在footer.php中)。困惑... – AleV