為 WordPress 分類目錄的描述添加可視化編輯器

默認情況下,WordPress後臺的 分類目錄 的描述隻能添加純文本內容,今天分享一下為 WordPress 分類目錄的描述添加可視化編輯器的方法。

tinymce-editor-category-description-wpdaxue_com

將下面的代碼添加到當前主題的 functions.php 即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
 * 為 WordPress 分類目錄的描述添加可視化編輯器
 * https://www.wpdaxue.com/add-tinymce-editor-category-description.html
 */
// 移除HTML過濾
remove_filter( 'pre_term_description', 'wp_filter_kses' );
remove_filter( 'term_description', 'wp_kses_data' );
//為分類編輯界面添加可視化編輯器的“描述”框
add_filter('edit_category_form_fields', 'cat_description');
function cat_description($tag)
{
	?>
	<table class="form-table">
		<tr class="form-field">
			<th scope="row" valign="top"><label for="description"><?php _ex('Description', 'Taxonomy Description'); ?></label></th>
			<td>
				<?php
				$settings = array('wpautop' => true, 'media_buttons' => true, 'quicktags' => true, 'textarea_rows' => '15', 'textarea_name' => 'description' );
				wp_editor(wp_kses_post($tag->description , ENT_QUOTES, 'UTF-8'), 'cat_description', $settings);
				?>
				<br />
				<span class="description"><?php _e('The description is not prominent by default; however, some themes may show it.'); ?></span>
			</td>
		</tr>
	</table>
	<?php
}
//移除默認的“描述”框
add_action('admin_head', 'remove_default_category_description');
function remove_default_category_description()
{
	global $current_screen;
	if ( $current_screen->id == 'edit-category' )
	{
		?>
		<script type="text/javascript">
			jQuery(function($) {
				$('textarea#description').closest('tr.form-field').remove();
			});
		</script>
		<?php
	}
}

/**
* 為 WordPress 分類目錄的描述添加可視化編輯器
* https://www.wpdaxue.com/add-tinymce-editor-category-description.html
*/
// 移除HTML過濾
remove_filter( ‘pre_term_description’, ‘wp_filter_kses’ );
remove_filter( ‘term_description’, ‘wp_kses_data’ );
//為分類編輯界面添加可視化編輯器的“描述”框
add_filter(‘edit_category_form_fields’, ‘cat_description’);
function cat_description($tag)
{
?>
<table class="form-table">
<tr class="form-field">
<th scope="row" valign="top"><label for="description"><?php _ex(‘Description’, ‘Taxonomy Description’); ?></label></th>
<td>
<?php
$settings = array(‘wpautop’ => true, ‘media_buttons’ => true, ‘quicktags’ => true, ‘textarea_rows’ => ’15’, ‘textarea_name’ => ‘description’ );
wp_editor(wp_kses_post($tag->description , ENT_QUOTES, ‘UTF-8’), ‘cat_description’, $settings);
?>
<br />
<span class="description"><?php _e(‘The description is not prominent by default; however, some themes may show it.’); ?></span>
</td>
</tr>
</table>
<?php
}
//移除默認的“描述”框
add_action(‘admin_head’, ‘remove_default_category_description’);
function remove_default_category_description()
{
global $current_screen;
if ( $current_screen->id == ‘edit-category’ )
{
?>
<script type="text/javascript">
jQuery(function($) {
$(‘textarea#description’).closest(‘tr.form-field’).remove();
});
</script>
<?php
}
}

上面的代碼分為三部分:

1.移除WP默認對描述內容的HTML代碼過濾功能

2.添加可視化編輯器的“描述”框

3.移除默認的“描述”框

註:該方法隻在“分類”的編輯頁面生效。

參考資料:http://www.paulund.co.uk/add-tinymce-editor-category-description

發佈留言