首页 > WordPress > 开发笔记 > WordPress如何在分类自定义字段添加默认编辑器

WordPress如何在分类自定义字段添加默认编辑器

一个老不正经 2023/11/14 478围观

在做WordPress企业站开发的时候,会碰到很多分类下面带html的单独描述,但是一般自定义字段是不会有html的,在下就遇到不少这样的设计稿,下面是撸好的自定义字段添加wp默认编辑器的代码。



首先在分类编辑页面添加自定义字段

function add_category_custom_fields() {
?>
<div class="form-field">
<label for="category_custom_field">自定义字段</label>
<?php wp_editor('', 'category_custom_field', array('textarea_name' => 'category_custom_field', 'wpautop' => true)); ?>
<p>请输入自定义字段的内容。</p>
</div>
<?php
}
add_action('category_add_form_fields', 'add_category_custom_fields');

然后在分类编辑页面显示自定义字段

function edit_category_custom_fields($tag) {
$category_custom_field = get_term_meta($tag->term_id, 'category_custom_field', true);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="category_custom_field">自定义字段</label></th>
<td><?php wp_editor($category_custom_field, 'category_custom_field', array('textarea_name' => 'category_custom_field', 'wpautop' => true)); ?></td>
</tr>
<?php
}
add_action('category_edit_form_fields', 'edit_category_custom_fields');

保存自定义字段数据

function save_category_custom_fields($term_id) {
if (isset($_POST['category_custom_field'])) {
update_term_meta($term_id, 'category_custom_field', $_POST['category_custom_field']);
}
}
add_action('edited_category', 'save_category_custom_fields');
add_action('create_category', 'save_category_custom_fields');

封装输出分类的自定义字段内容

function display_category_custom_field() {
$category_custom_field = get_term_meta(get_queried_object()->term_id, ‘category_custom_field’, true);
echo wpautop($category_custom_field);
}


调用封装代码即可

<?php display_category_custom_field(); ?>

完整的分类自定义字段添加默认编辑器代码

<?php
// 在分类编辑页面添加自定义字段
function add_category_custom_fields() {
?>
<div class="form-field">
<label for="category_custom_field">自定义字段</label>
<?php wp_editor('', 'category_custom_field', array('textarea_name' => 'category_custom_field', 'wpautop' => true)); ?>
<p>请输入自定义字段的内容。</p>
</div>
<?php
}
add_action('category_add_form_fields', 'add_category_custom_fields');

// 在分类编辑页面显示自定义字段
function edit_category_custom_fields($tag) {
$category_custom_field = get_term_meta($tag->term_id, 'category_custom_field', true);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="category_custom_field">自定义字段</label></th>
<td><?php wp_editor($category_custom_field, 'category_custom_field', array('textarea_name' => 'category_custom_field', 'wpautop' => true)); ?></td>
</tr>
<?php
}
add_action('category_edit_form_fields', 'edit_category_custom_fields');

// 保存自定义字段数据
function save_category_custom_fields($term_id) {
if (isset($_POST['category_custom_field'])) {
update_term_meta($term_id, 'category_custom_field', $_POST['category_custom_field']);
}
}
add_action('edited_category', 'save_category_custom_fields');
add_action('create_category', 'save_category_custom_fields');

// 输出分类的自定义字段内容
function display_category_custom_field() {
$category_custom_field = get_term_meta(get_queried_object()->term_id, 'category_custom_field', true);
echo wpautop($category_custom_field);
}