没有不值得去解决的问题,也没有不值得去学习的技术!

Stop Wasting Time: A Complete Guide to Automating WordPress Code Blocks, from Manual Adjustments to Error Fixes

When writing technical blog posts, displaying code blocks correctly is essential. Recently, I found the process of handling code blocks in the WordPress Gutenberg editor inefficient and started looking for a better approach. After repeated experimentation, I eventually worked out a complete workflow that moves from manual handling to automation. It resolves parsing errors while significantly improving efficiency. I am sharing the process and solution here in case you are dealing with the same code block issues. Previous approach: A Practical Record of Fixing Unstable Parsing of Multiple Shortcodes in WordPress Gutenberg

1. Current Approach: An Inefficient but Workable “Manual Three-Step Method”

At the moment, I handle code blocks as follows:

1. Step 1: Paste

Plaintext
[go][/go]

shortcode
In the visual editor, first paste

Plaintext
[go]你的代码块内容[/go]

. The editor automatically converts it into a predefined shortcode block (such as a <!-- wp:shortcode --> block).
2. Step 2: Manually delete the shortcode block (Figure 1) and replace it with a SyntaxHighlighter block

Manually delete the shortcode block (Figure 1)


Find the <!-- wp:shortcode --> block that was just converted, delete it manually, and replace it with the following (Figure 2):

Replace it with a SyntaxHighlighter block
Plaintext
<code>

<!-- wp:syntaxhighlighter/code {"language":"go"} -->

<pre class="wp-block-syntaxhighlighter-code">你的代码块内容</pre>

<!-- /wp:syntaxhighlighter/code -->

</code>

3. Step 3: Adjust the language attribute

If syntax highlighting is required instead of the default Plain Text, you also need to manually add an attribute such as {"language":"go"}.

Problem: This is extremely inefficient, especially when an article contains multiple code blocks.

If an article contains more than 10 code blocks, repeating the three steps above takes a lot of time and is prone to mistakes. I needed a more efficient approach.

2. Exploration and Frustration: Pasting Code Directly Causes Errors

To improve efficiency, I tried using the <!-- wp:syntaxhighlighter/code --> block shown in the code editor as a reference (Figure 3). For example:

To improve efficiency, I tried using the <!-- wp:syntaxhighlighter/code --> block shown in the code editor as a reference (Figure 3)
Plaintext
<!-- wp:syntaxhighlighter/code {"language":"go"} -->
<pre class="wp-block-syntaxhighlighter-code">
// Walk 中序遍历树 t
func Walk(t *tree.Tree, ch chan int) {
    ch <- t.Value // 这里包含 < 符号!
}
</pre>
<!-- /wp:syntaxhighlighter/code -->

However, when the code contains unescaped `<` or `>` characters (such as the `<` in `ch <- t.Value`), the editor reports an error:

The block contains unexpected or invalid content (Figure 4)
Reason: WordPress misinterprets `<` or `>` as HTML tags, causing parsing to fail.

When the code contains unescaped `<` or `>` characters (such as the `<` in `ch <- t.Value`), the editor reports an error: The block contains unexpected or invalid content (Figure 4)

3. Core Solution: An Automated Escaping Workflow

After several rounds of testing, I found an automated approach that requires no manual escaping and solves both the parsing-error and efficiency problems. The core idea is to use a text editor (EditPlus) to batch-replace special characters in the code while keeping the block comment syntax unchanged.

4. Detailed Steps (Completed Entirely in the Text Editor)

1. Step 1: Write the Original Content in the Text Editor

Use custom markers such as text and go to separate code blocks while preserving the original code formatting:

Plaintext
我正在学习 Go 语言的并发编程。

[text]
# 检查ufw是否在运行
root@wireguard-2026-0509:~# ufw status
Status: inactive
[/text]

[go]
// Walk 中序遍历树 t
func Walk(t *tree.Tree, ch chan int) {
    ch <- t.Value // 这里包含 < 符号!
}
[/go]

2. Step 2: Batch-Replace the Markers with Code Block Templates

Plaintext
Replace `[text]</code>` with:</p> <!-- /wp:paragraph --> <!-- wp:shortcode --> <!-- wp:syntaxhighlighter/code --> <pre class="wp-block-syntaxhighlighter-code"> <!-- /wp:shortcode --> <!-- wp:paragraph --> <p>Replace `<code>[/text]` with:</pre>
<!-- /wp:syntaxhighlighter/code -->
Replace `[go]</code>` with:</p> <!-- /wp:paragraph --> <!-- wp:shortcode --> <!-- wp:syntaxhighlighter/code {"language":"go"} --> <pre class="wp-block-syntaxhighlighter-code"> <!-- /wp:shortcode --> <!-- wp:paragraph --> <p>Replace `<code>[/go]` with:</pre>
<!-- /wp:syntaxhighlighter/code -->

3. Step 3: Precisely Escape Special Characters in the Code Content (Figure 5)

Step 3: Precisely escape special characters in the code content (Figure 5)


– Key point: Select only

Plaintext
<pre>

the code content inside the tag (excluding block comments), and perform the following replacements:

Plaintext
<!-- wp:syntaxhighlighter/code {"language":"go"} -->
<pre class="wp-block-syntaxhighlighter-code">
func Walk(t *tree.Tree, ch chan int) {
    ch <- t.Value // 报错!
}
</pre>
<!-- /wp:syntaxhighlighter/code -->

After replacement (displayed correctly) (Figure 6):

After replacement (displayed correctly) (Figure 6)
Plaintext
<!-- wp:syntaxhighlighter/code {"language":"go"} -->
<pre class="wp-block-syntaxhighlighter-code">
func Walk(t *tree.Tree, ch chan int) {
    ch <- t.Value // 转义后正确解析
}
</pre>
<!-- /wp:syntaxhighlighter/code -->

6. Important Notes

1. Escape only the code content, not the block comments! (In other words, do not escape

Plaintext
<!-- wp:... -->

any characters inside the tag.)


2. The front end displays correctly: Although the code contains &lt;, the browser automatically renders it as < after publishing, so readers see the correct code.

3. Efficiency improvement: Compared with manually adjusting each code block (5–10 minutes per 10 blocks), this approach takes only about 30 seconds per 10 blocks.

7. Summary and Recommendations

1. Moving from manual work to automation: Batch replacement in a text editor eliminates the parsing errors while greatly improving processing efficiency.

2. Recommended workflow: Save the replacement templates in the text editor. Each time you write a blog post, paste the templates and follow the same steps.

3. Best use case: This approach is particularly useful for technical bloggers writing articles with many code blocks. I hope it helps you reduce repetitive manual work and write technical posts more efficiently.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系