word转html <pre>标签,wordpress编辑器增加pre自定义标签

wordpress输入带格式的代码时,每次都需要手动输入pre标签很不方便。

如何能够像编辑器带的b标签一样,点一下就自动输入。

wordpress增加自定义标签办法:

修改主题的functions.php文件,路径为:

/网站路径/wp-content/themes/主题文件夹/functions.php,

然后将以下代码添加<?php 之后:

//添加HTML编辑器自定义快捷标签按钮

add_action('after_wp_tiny_mce', 'add_button_mce');

function add_button_mce($mce_settings) {

?>

QTags.addButton( 'hr', 'hr', "n


n", "" );

QTags.addButton( 'h1', 'h1', "n

", "

n" );

QTags.addButton( 'h2', 'h2', "n

", "

n" );

QTags.addButton( 'h3', 'h3', "n

", "

n" );

QTags.addButton( 'pre', 'pre', "n<pre>n", "n</pre>n" );

}

1

2

3

4

5

6

7

8

9

10

11

12

13//添加HTML编辑器自定义快捷标签按钮

add_action('after_wp_tiny_mce','add_button_mce');

functionadd_button_mce($mce_settings){

?>

QTags.addButton('hr','hr',"n


n","");

QTags.addButton('h1','h1',"n

","

n");

QTags.addButton('h2','h2',"n

","

n");

QTags.addButton('h3','h3',"n

","

n");

QTags.addButton('pre','pre',"n<pre>n","n</pre>n");

}

addButton的四个参数:分别表示按钮的ID、按钮显示名、点一下输入内容、再点一下关闭内容(空则一次输入全部内容),n表示换行

可以使用 QTags.addButton( '', '', '', '' )增加多个按钮!

效果如下:

21d86802bd4eb1491b53964ac5478522.png

wordpre pre标签内的html代码转义

想在wordpress 文章中插入代码,例如php的,发现被浏览器解析了,所以我们要在输出前对代码进行转义,这样pre输出就正常了。

以下内容放在functions.php的开头

// convert htmlentity for pre tag

add_filter('the_content', 'htmlspecialchars_pre', 12);

add_filter('get_comment_text', 'htmlspecialchars_pre');

function htmlspecialchars_pre ($content) {

return preg_replace_callback ("

(.*?)/si", create_function('$matches','return "" . htmls_pecial_chars($matches[1]) ."";'),$content);

}

function htmls_pecial_chars($content=''){

$content = str_replace("

$content = str_replace(">",">",$content);

$content = str_replace("&","&",$content);

$content = str_replace('"',""",$content);

$content = str_replace("'","'",$content);

$content = str_replace(" "," ",$content);

return $content;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16// convert htmlentity for pre tag

add_filter('the_content','htmlspecialchars_pre',12);

add_filter('get_comment_text','htmlspecialchars_pre');

functionhtmlspecialchars_pre($content){

returnpreg_replace_callback("

(.*?)/si",create_function('$matches','return "" . htmls_pecial_chars($matches[1]) ."";'),$content);

}

functionhtmls_pecial_chars($content=''){

$content=str_replace("

$content=str_replace(">",">",$content);

$content=str_replace("&","&",$content);

$content=str_replace('"',""",$content);

$content=str_replace("'","'",$content);

$content=str_replace(" "," ",$content);

return$content;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值