代码块(兼容带属性)
$pattern = '/(]*?>|)\s*]*?>(.*?)<\/code>\s*(<\/pre>)/is';
$content = preg_replace_callback( $pattern, function( $matches ) {
$code_content = $matches[2];
// 步骤1:彻底解码所有多重转义(先还原原始内容)
do {
$original = $code_content;
// 解码所有HTML实体
$code_content = html_entity_decode(
$code_content,
ENT_QUOTES | ENT_HTML5,
'UTF-8'
);
// 兜底处理双层转义
$code_content = str_replace(
array('&', ''', '"'),
array('&', "'", '"'),
$code_content
);
} while ( $code_content !== $original );
// 步骤2:关键!将代码块内的< >转成可显示的实体(避免被浏览器解析)
// 这样会显示为文本,而不是被解析成HTML段落
$code_content = str_replace(
array('<', '>'),
array('<', '>'),
$code_content
);
// 步骤3:确保引号是正常显示的(不是实体)
$code_content = str_replace(
array(''', '"'),
array("'", '"'),
$code_content
);
// 重新拼接代码块
return $matches[1] . '' . $code_content . '' . $matches[3];
}, $content );
return $content;
}
// 高优先级执行
add_filter( 'the_content', 'fix_githuber_md_show_tag_text', 20 );
add_filter( 'the_excerpt', 'fix_githuber_md_show_tag_text', 20 );
/**
* 精准控度修复:只移除插件多套的转义,保留代码本身的转义实体
* 解决代码内容被过度解码、篡改的问题
*/
function fix_githuber_md_precise_escape_only( $content ) {
// 匹配所有
代码块(保留所有标签属性)
$pattern = '/(]*?>)\s*]*?>(.*?)<\/code>\s*(<\/pre>)/is';
$content = preg_replace_callback( $pattern, function( $matches ) {
$pre_tag = $matches[1];
$code_content = $matches[2];
$pre_close = $matches[3];
// 核心:只解码「插件多套的那一层转义」,保留代码本身的转义实体
// 只处理 & → & | ' → ' | " → "
$code_content = str_replace(
array('&', ''', '"', '<', '>'),
array('&', ''', '"', '<', '>'),
$code_content
);
// 重新拼接:保留所有标签属性 + 还原后的代码内容
return $pre_tag . '' . $code_content . '' . $pre_close;
}, $content );
return $content;
}
// 优先级20:确保在插件处理完后执行
add_filter( 'the_content', 'fix_githuber_md_precise_escape_only', 20 );
add_filter( 'the_excerpt', 'fix_githuber_md_precise_escape_only', 20 );