出於安全等因素考慮,WordPress 後臺的文本框一般是不允許添加 html 代碼的(也就是被過濾掉)。
最近有用戶需要在分類描述中添加 html 代碼,下面分享一下實現方法。
直接將下面的代碼添加到當前主題的 functions.php 文件即可:
1 2 3 4 5 6 |
/** * 允許分類描述添加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'); |
/**
* 允許分類描述添加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:
1 2 3 4 5 6 7 8 9 |
// 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'); } |
// 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’);
}