WordPress具有自動設置代碼格式的習慣,這對於某些博主而言可能是一個巨大的問題。 您可以使用“語法突出顯示插件”或手動對所有代碼進行編碼,但是這些方式都有其自身的缺點。 最近在客戶網站上工作時,我們發現了一個有用的技巧,該技巧將通過使用簡碼來禁用WordPress帖子中的自動格式設置。
首先,您需要打開主題的 functions.php 文件並粘貼以下代碼:
function my_formatter($content) { $new_content=""; $pattern_full="{([raw].*?[/raw])}is"; $pattern_contents="{[raw](.*?)[/raw]}is"; $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ($pieces as $piece) { if (preg_match($pattern_contents, $piece, $matches)) { $new_content .= $matches[1]; } else { $new_content .= wptexturize(wpautop($piece)); } } return $new_content; } remove_filter('the_content', 'wpautop'); remove_filter('the_content', 'wptexturize'); add_filter('the_content', 'my_formatter', 99);
一旦粘貼了上面的代碼並上傳了文件,就可以使用簡碼了。 在撰寫文章時,只需使用以下簡碼:
[raw]Unformatted code[/raw]
如果你有任何疑問,請告訴我們。
資料來源:WPRecipes