如何在WordPress自定义分类法及插件表单中添加字段并展示已保存数据?
Hey there! Let's walk through the step-by-step technical solution to add custom fields to your WordPress custom taxonomy—both storing the data and making them work seamlessly in the edit form just like the default name, slug, and description fields.
1. 注册并存储自定义字段数据
First, we'll hook into WordPress taxonomy actions to add form fields and save the input data to the database. We'll use a hypothetical custom taxonomy named book_category—replace this with your actual taxonomy slug.
1.1 添加「新建分类」页面的自定义字段
This adds the field to the form when creating a new taxonomy term:
// Hook into the add form for our custom taxonomy add_action('book_category_add_form_fields', 'render_add_custom_taxonomy_field', 10, 1); function render_add_custom_taxonomy_field($taxonomy) { ?> <div class="form-field term-custom-field-wrap"> <label for="category_featured_label"><?php _e('Featured Label', 'your-plugin-text-domain'); ?></label> <input type="text" name="category_featured_label" id="category_featured_label" value="" /> <p class="description"><?php _e('Enter a featured label for this category (e.g., "Bestseller")', 'your-plugin-text-domain'); ?></p> </div> <?php }
1.2 添加「编辑分类」页面的自定义字段(并显示已保存数据)
This renders the field on the edit term page, pre-populated with the saved value:
// Hook into the edit form for our custom taxonomy add_action('book_category_edit_form_fields', 'render_edit_custom_taxonomy_field', 10, 2); function render_edit_custom_taxonomy_field($term, $taxonomy) { // Retrieve the saved meta value $saved_label = get_term_meta($term->term_id, 'category_featured_label', true); ?> <tr class="form-field term-custom-field-wrap"> <th scope="row"><label for="category_featured_label"><?php _e('Featured Label', 'your-plugin-text-domain'); ?></label></th> <td> <input type="text" name="category_featured_label" id="category_featured_label" value="<?php echo esc_attr($saved_label); ?>" /> <p class="description"><?php _e('Enter a featured label for this category (e.g., "Bestseller")', 'your-plugin-text-domain'); ?></p> </td> </tr> <?php }
1.3 保存自定义字段数据
We'll use two hooks to handle saving data when creating or updating a term:
// Save data when a new term is created add_action('created_book_category', 'save_custom_taxonomy_field', 10, 2); // Save data when an existing term is updated add_action('edited_book_category', 'save_custom_taxonomy_field', 10, 2); function save_custom_taxonomy_field($term_id, $taxonomy) { // Check if our field is present in the POST data if (!isset($_POST['category_featured_label'])) { return; } // Sanitize the input to prevent security issues $cleaned_label = sanitize_text_field($_POST['category_featured_label']); // Update or add the term meta update_term_meta($term_id, 'category_featured_label', $cleaned_label); }
2. 让自定义字段像默认字段一样展示
To make the field behave like default taxonomy fields (e.g., showing up in the term list table), add these hooks:
2.1 添加分类列表页的自定义列
// Add a new column to the taxonomy term list add_filter('manage_edit-book_category_columns', 'add_custom_taxonomy_list_column'); function add_custom_taxonomy_list_column($columns) { $columns['category_featured_label'] = __('Featured Label', 'your-plugin-text-domain'); return $columns; }
2.2 填充列表列的内容
// Populate the custom column with saved data add_action('manage_book_category_custom_column', 'populate_custom_taxonomy_list_column', 10, 3); function populate_custom_taxonomy_list_column($content, $column_name, $term_id) { if ($column_name === 'category_featured_label') { $saved_label = get_term_meta($term_id, 'category_featured_label', true); echo esc_html($saved_label ?: '-'); // Show a dash if no value exists } }
关键注意事项
- Replace
book_categorywith your actual custom taxonomy slug everywhere in the code. - Replace
category_featured_labelwith your desired field key (use a unique name to avoid conflicts). - Replace
your-plugin-text-domainwith your plugin/theme's text domain for internationalization. - Use appropriate sanitization functions: use
sanitize_textarea_fieldfor multi-line text,absintfor numbers, etc. - If you're adding this to a custom plugin, place all code in your plugin's main file or a dedicated include file—ensure it runs after WordPress initializes.
内容的提问来源于stack exchange,提问作者Prits

