WordPress 6.5.3中wp_editor双引号被错误替换为弯引号求助
WordPress 6.5.3 wp_editor(TinyMCE)双引号错误替换问题解决方案
问题重现
调用wp_editor加载内容时,HTML实体"被替换为中文引号“,切换可视化编辑器后会出现嵌套引号(如src="“xxx“"),导致显示异常;提前移除wptexturize等过滤器无效。
解决步骤
1. 精准控制过滤器的移除与恢复
wp_editor内部会通过the_editor_content过滤器处理内容,需在调用编辑器前后精准移除和恢复过滤器,避免影响全局功能:
// 调用wp_editor前,移除会转换引号的过滤器(指定默认优先级10) remove_filter('the_editor_content', 'wptexturize', 10); remove_filter('the_editor_content', 'wp_filter_post_kses', 10); remove_filter('the_editor_content', 'wp_filter_kses', 10); // 渲染编辑器 wp_editor($editable_content, 'post_content', [ 'textarea_name' => 'post_content', 'textarea_rows' => 20, ]); // 调用后恢复过滤器,防止影响其他编辑器或内容输出 add_filter('the_editor_content', 'wptexturize', 10); add_filter('the_editor_content', 'wp_filter_post_kses', 10); add_filter('the_editor_content', 'wp_filter_kses', 10);
2. 配置TinyMCE禁用智能引号转换
在wp_editor的参数中添加TinyMCE配置,明确禁用智能标点转换,同时限制实体编码范围:
wp_editor($editable_content, 'post_content', [ 'textarea_name' => 'post_content', 'textarea_rows' => 20, 'tinymce' => [ 'smart_punctuation' => false, // 核心:禁用智能引号、破折号转换 'entity_encoding' => 'raw', // 实体编码设为raw,避免额外转义 'entities' => '160,60,62,38,34', // 只保留必要HTML实体 'convert_urls' => false, // 禁止自动转换URL格式 ], ]);
3. 检查短代码回调的内容处理
确保WPCode中短代码的回调函数未对$editable_content做额外格式化,比如避免调用wptexturize、wpautop等函数:
function custom_editable_shortcode($atts) { $editable_content = get_post_meta(get_the_ID(), 'your_editable_meta_key', true); ob_start(); // 这里放入上述过滤器控制+wp_editor代码 remove_filter('the_editor_content', 'wptexturize', 10); wp_editor($editable_content, 'post_content', [ 'textarea_name' => 'post_content', 'tinymce' => ['smart_punctuation' => false] ]); add_filter('the_editor_content', 'wptexturize', 10); return ob_get_clean(); } add_shortcode('editable_content_area', 'custom_editable_shortcode');
4. 排查插件冲突
暂时禁用编辑器增强类、文本格式化类插件,测试问题是否消失,确认是否有第三方插件干扰引号转换逻辑。
内容的提问来源于stack exchange,提问作者hobbycode
相关产品推荐
相关产品推荐

