如何禁用WordPress帖子中的自动格式

WordPress有自动格式化代码的习惯,对于一些博客来说这可能成为一个巨大的问题。您可以使用语法Highlighter插件或手动编码所有代码,但这些方法有其自身的缺点。最近在客户的网站上工作,我们发现了一个有用的技巧,它将通过使用短代码禁用WordPress帖子中的自动格式化。

首先,您需要打开主题的 functions.php 文件并粘贴以下代码:

      function my_formatter($content) {      $new_content = "";      $pattern_full = "{(
.*?[/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);  </pre><p>粘贴上面的代码后上传文件,然后您就可以使用短代码了。在撰写帖子时,只需使用下面的短代码:</p><pre clear">        

WordPress有自动格式化代码的习惯,对于一些博客来说这可能成为一个巨大的问题。您可以使用语法Highlighter插件或手动编码所有代码,但这些方法有其自身的缺点。最近在客户的网站上工作,我们发现了一个有用的技巧,它将通过使用短代码禁用WordPress帖子中的自动格式化。

首先,您需要打开主题的 functions.php 文件并粘贴以下代码:

      function my_formatter($content) {      $new_content = "";      $pattern_full = "{(
.*?[/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);  </pre><p>粘贴上面的代码后上传文件,然后您就可以使用短代码了。在撰写帖子时,只需使用下面的短代码:</p><pre class="EnlighterJSRAW" data-enlighter-language="null">  [raw]Unformatted code

如果您有任何疑问,请告诉我们。

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-05-10 12:51:48
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: