首页 > 技术 > 正文

#教程# WordPress - 在文章中插入HTML/PHP代码

如果不使用插件的话,在文章中和评论中是无法插入一些 PHP、CSS、HTML、JS 等代码的,就算使用了code或pre标签也不行…因为 wordpress 自身有一个强大的 HTML 标签过滤系统,你插入的 html 标签会直接消失;

如果不使用插件的话,在文章中和评论中是无法插入一些 PHP、CSS、HTML、JS 等代码的,就算使用了 code 或 pre 标签也不行,因为 WordPress 自身有一个强大的 HTML 标签过滤系统,你插入的 html 或者 PHP 标签会直接消失;

将下面的代码加入主题的 functions.php 中,然后使用 code 或者 pre 标签来插入代码即可:

// 在文章和评论中插入代码

add_filter('pre_comment_content','lxtx_encode_code_in_posts_comments');

add_filter('the_content','lxtx_encode_code_in_posts_comments');

function lxtx_encode_code_in_posts_comments($source) {

$encoded = preg_replace_callback('/<code>(.*?)</code>/ims',

create_function(

'$matches',

'$matches[1] = preg_replace(

array("/^[r|n]+/i","/[r|n]+$/i"),"",

$matches[1]);

return "<code>" . esc_html( $matches[1] ) . "</code>";'

),

$source);

if ($encoded)

return $encoded;

else

return $source;

}

网友评论

验证码 换一张
取 消
暂无评论...
为您推荐
  • 相关阅读
  • 业界资讯
  • 手机通讯
  • 电脑办公
  • 新奇数码
  • 软件游戏
  • 科学探索