小心肝天宝 發表於 2023-10-3 00:00:00

WordPress中让Widget 标题支持简单的HTML标签

<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
代码如下:</div>
<div id="phpcode14" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;?php<br>
/*<br>
Plugin Name: WPJAM HTML Widget Title<br>
Description: 让 Widget 标题支持简单的 HTML 标签<br>
Version: 0.1<br>
Author: Denis<br>
*/<br>
add_filter( 'widget_title', 'wpjam_html_widget_title' );<br>
function wpjam_html_widget_title( $title ) {<br>
//HTML tag opening/closing brackets<br>
$title = str_replace( '[', '&lt;', $title );<br>
$title = str_replace( '[/', '&lt;/', $title );&lt;/p&gt; &lt;p&gt; //&lt;strong&gt;&lt;/strong&gt;<br>
$title = str_replace( 's]', 'strong&gt;', $title );<br>
//&lt;em&gt;&lt;/em&gt;<br>
$title = str_replace( 'e]', 'em&gt;', $title );&lt;/p&gt; &lt;p&gt; return $title;<br>
}<br>
?&gt;</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
把上面的代码复制到当前主题的 functions.php 文件或者直接保存插件上传之后,就可以在 Widget 标题输入类似的代码:“测试粗体和斜体”实现粗体和斜体的 HTML 标签。上面的代码让 Widget 标题仅仅支持加粗和斜体两种 HTML 标签,但是我们可以通过相同的方法非常容易自己添加其他的 HTML 标签支持。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
如果是普通的php中想让标题支持html标签可以使用如下的代码</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode15" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
$title = str_replace( '&lt;', '&amp;lt;', $title);<br>
$title = str_replace( '&gt;', '&amp;gt;', $title);<br>
$title = cn_substr($title,80);</div>
頁: [1]
查看完整版本: WordPress中让Widget 标题支持简单的HTML标签