出于安全等因素考虑,WordPress 后台的文本框一般是不允许添加 html 代码的(也就是被过滤掉)。

最近有用户需要在分类描述中添加 html 代码,下面分享一下实现方法。
直接将下面的代码添加到当前主题的 functions.php 文件即可:
|
|
<span style="color: #009933; font-style: italic;">/** * 允许分类描述添加html代码 * https://www.wpdaxue.com/category-description-support-html.html */</span> remove_filter<span style="color: #009900;">(</span><span style="color: #0000ff;">'pre_term_description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wp_filter_kses'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> remove_filter<span style="color: #009900;">(</span><span style="color: #0000ff;">'term_description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wp_kses_data'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> |
|
/**
* 允许分类描述添加html代码
* https://www.wpdaxue.com/category-description-support-html.html
*/
remove_filter(‘pre_term_description’, ‘wp_filter_kses’);
remove_filter(‘term_description’, ‘wp_kses_data’);
如果你需要进一步了解实现原理,可以自己阅读以下文档:
http://codex.wordpress.org/Function_Reference/wp_filter_kses
http://codex.wordpress.org/Function_Reference/wp_kses_data
如果你还想让 链接描述和备注、用户描述 也一样支持 html 代码,可以试试下面的代码,同样是添加到functions.php:
|
|
<span style="color: #666666; font-style: italic;">// Disables Kses only for textarea saves</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">(</span><span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'pre_term_description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pre_link_description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pre_link_notes'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pre_user_description'</span><span style="color: #009900;">)</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$filter</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> remove_filter<span style="color: #009900;">(</span><span style="color: #000088;">$filter</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wp_filter_kses'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> <span style="color: #666666; font-style: italic;">// Disables Kses only for textarea admin displays</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">(</span><span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'term_description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'link_description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'link_notes'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user_description'</span><span style="color: #009900;">)</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$filter</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span> remove_filter<span style="color: #009900;">(</span><span style="color: #000088;">$filter</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wp_kses_data'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span> |
|
// Disables Kses only for textarea saves
foreach (array(‘pre_term_description’, ‘pre_link_description’, ‘pre_link_notes’, ‘pre_user_description’) as $filter) {
remove_filter($filter, ‘wp_filter_kses’);
} // Disables Kses only for textarea admin displays
foreach (array(‘term_description’, ‘link_description’, ‘link_notes’, ‘user_description’) as $filter) {
remove_filter($filter, ‘wp_kses_data’);
}
赏 如果本文对你有帮助,请打赏作者,鼓励我们继续写作!
请先
!