When displaying code snippets containing Emoji in WordPress, you often encounter the issue of Emoji being converted into
🎉or<img>tags. This article documents a complete troubleshooting and resolution process.
Issue Description
In a WordPress Gutenberg editor code block, write the following PHP code, as shown in Figure 1:

echo "\n🎉 全部处理完成!\n";
echo "📊 统计:\n";
echo " 已处理新标签: {$processed}\n";
echo " 已跳过已有翻译: {$skipped}\n";
echo "\n✅ 所有标签都已经处理完毕,和手动添加的完全一样!\n";After saving the post, the front end displays:
echo "\n🎉 全部处理完成!\n";
echo "📊 统计:\n";
...Or worse, the Emoji turns into a long <img> tag:
echo "\n<img draggable="false" ... src=".../1f389.svg"> 全部处理完成!\n";As shown in Figure 2: Emoji displays normally in the editor

As shown in Figure 3: Emoji turns into HTML entities in the front-end code block

As shown in Figure 8: Emoji turns into img tags in the front-end code block

Cause Analysis
This issue is caused by the combination of two independent mechanisms:
- Database character set does not support Emoji
Theutf8character set in older versions of MySQL only supports 3-byte characters, while Emoji occupies 4 bytes. When a table or field usesutf8, WordPress automatically converts Emoji into HTML entities (e.g.,🎉→🎉) before saving to avoid database errors. As shown in Figure 4: Query reveals the core table character set is utf8_general_ci - Front-end Emoji image replacement during output
Even if the database already supportsutf8mb4, WordPress still replaces Emoji characters with SVG images loaded from a CDN (via thewp_staticize_emojifilter) by default, causing<img>tags to appear inside code blocks.

For users of code highlighting plugins like SyntaxHighlighter Evolved, both of these conversions completely ruin the exact display of the code.
Solution
Step 1: Check and upgrade the database character set
Ensure your WordPress database uses the utf8mb4 character set.
- Check the current table character set via phpMyAdmin or an SQL query:
SELECT TABLE_NAME, TABLE_COLLATION
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = '你的数据库名';As shown in Figure 4: Example query results (some tables use utf8_general_ci)

- If core tables like
wp_posts,wp_options, andwp_termsshowutf8_general_ci, you need to convert them:
-- SELECT TABLE_NAME, TABLE_COLLATION FROM information_schema.TABLES WHERE TABLE_SCHEMA = '你的数据库名'; 的查询结果如下:
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cky_banners','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cky_cookie_categories','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cky_cookies','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cmplz_cookiebanners','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cmplz_cookies','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_cmplz_services','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_commentmeta','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_comments','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_links','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_options','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_orgseriesicons','utf8mb4_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_post_views','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_postmeta','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_posts','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_term_relationships','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_term_taxonomy','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_termmeta','utf8mb4_unicode_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_terms','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_usermeta','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_users','utf8_general_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_yoast_indexable','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_yoast_indexable_hierarchy','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_yoast_migrations','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_yoast_primary_term','utf8mb4_unicode_520_ci');
INSERT INTO `TABLES` (`TABLE_NAME`,`TABLE_COLLATION`) VALUES ('wp_yoast_seo_links','utf8_general_ci');
-- 需要转换的 SQL 如下:
-- 文章相关表
ALTER TABLE wp_posts CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE wp_postmeta CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 选项表(可能存有包含 Emoji 的选项值)
ALTER TABLE wp_options CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 分类/标签表
ALTER TABLE wp_terms CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE wp_term_taxonomy CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE wp_term_relationships CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 评论表(如果评论可能包含 Emoji)
ALTER TABLE wp_comments CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE wp_commentmeta CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 用户表(可选)
ALTER TABLE wp_users CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE wp_usermeta CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 友情链接表(如果有)
ALTER TABLE wp_links CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE wp_yoast_seo_links CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;As shown in Figure 5: Execute ALTER TABLE to convert


- Force the character set in
wp-config.php, as shown in Figure 7:
define('DB_CHARSET', 'utf8mb4');
define('DB_COLLATE', 'utf8mb4_unicode_ci');
⚠️ Note: Always back up your database before performing these operations!
Step 2: Disable front-end Emoji image replacement
Add the following code to your theme’s functions.php:
// 彻底禁用 WordPress 前端 Emoji 转换为图片
remove_filter( 'the_content', 'wp_staticize_emoji' );
remove_filter( 'the_excerpt', 'wp_staticize_emoji' );
remove_filter( 'comment_text', 'wp_staticize_emoji' );
// 同时移除 Emoji 的 JS/CSS(提升加载速度)
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );As shown in Figure 10: Add the disabling code in functions.php

Step 3: Clean up abnormal content in existing posts
- New posts: After configuring the previous two steps, simply saving with Emoji will display them normally.
- Old posts: If they were previously converted to HTML entities or
<img>tags, you need to manually re-edit them, replace the entities (e.g.,🎉) back to the original Emoji characters (🎉), and then update the post.
As shown in Figure 9: Replace entities with Emoji in the Gutenberg editor

As shown in Figure 11: Final front-end display result (Emoji displayed as-is)

Summary
| Issue | Cause | Solution |
|---|---|---|
Emoji turns into &#x...; after saving | Database character set is not utf8mb4 | Upgrade tables to utf8mb4 |
Front end displays <img> tags | wp_staticize_emoji filter | Remove the filter in functions.php |
By following these three steps, your WordPress site will perfectly display code snippets containing Emoji within code blocks, without being disrupted by any conversions. If you are using SyntaxHighlighter Evolved or other code highlighting plugins, this solution applies equally well.
This article is based on real troubleshooting records, and all operations have been verified in a WordPress 7.x + PHP 8.x environment.


评论
一条对“Emoji Display Issues in WordPress Code Blocks? Fix Them in Three Steps”的回复
[…] In old themes, emoji can be displayed normally in the code block using the SyntaxHighlighter Evolved plugin. This is because disabling the code will prevent WordPress from generating emoji in the form of pictures, so that the emoji (Unicode character) in the form of text is not converted and can be displayed normally. Reference:Emoji display exception in WordPress code block? Three-step solution […]