钩子文档

{$taxonomy}_edit_form_fields

💡 云策文档标注

概述

这是一个 WordPress 动作钩子,在编辑分类法术语的表单字段显示后触发。钩子名称包含动态部分 $taxonomy,允许针对特定分类法进行定制。

关键要点

  • 钩子名称格式为 {$taxonomy}_edit_form_fields,例如 category_edit_form_fields 或 post_tag_edit_form_fields
  • 参数包括 $tag(WP_Term 对象,当前术语)和 $taxonomy(字符串,当前分类法 slug)
  • 自 WordPress 3.0.0 版本引入,用于在编辑术语页面添加自定义字段或修改现有字段

代码示例

// 示例:为分类法添加自定义元字段(如 category)
$prefix_taxonomy = 'category';
function wporg_prefix_add_meta_fields() {
    ?>
    <div class="form-field">
        <label for="wporg_field">Custom Field</label>
        <input type="text" name="wporg_field[]" id="wporg_field" value="">
        <p class="description">Description for custom field.</p>
    </div>
    <?php
}
add_action('category_edit_form_fields', 'wporg_prefix_add_meta_fields');

注意事项

  • 钩子名称中的 $taxonomy 必须替换为实际分类法 slug,以确保正确触发
  • 用户贡献笔记提供了添加自定义元字段和使用 WP 编辑器增强描述字段的实用示例,但需注意代码适配性和安全性

📄 原文内容

Fires after the Edit Term form fields are displayed.

Description

The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug.

Possible hook names include:

  • category_edit_form_fields
  • post_tag_edit_form_fields

Parameters

$tagWP_Term
Current taxonomy term object.
$taxonomystring
Current taxonomy slug.

Source

do_action( "{$taxonomy}_edit_form_fields", $tag, $taxonomy );

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Adding custom meta fields to a taxonomy.

    Note that in this example name of the input is set to be an array. If the field name was not an array, then you’d have to save each field individually.

    // Key of your custom taxonomy goes here.
    // Taxonomy key, must not exceed 32 characters.
    $prefix_taxonomy = 'category';
    
    /**
     * This will add the custom meta field to the add new term page.
     *
     * @return void
     */
    function wporg_prefix_add_meta_fields() {
    	?>
    
    	

  2. Skip to note 4 content

    Adding WP editor to default description field:

    /**
     * Default taxonomy description field using WP editor
     *
     * @link    https://codex.wordpress.org/Javascript_Reference/wp.editor
     * @return  void
     */
    function html_taxonomy_description()
    {
    	?>
    	
    		jQuery(document).ready(function($) {
    			let field = 'tag-description';
    			if ( document.getElementById(field) == undefined ) {
    				field = 'description';
    			}
    
    			wp.editor.initialize(field, {
    				tinymce: {
    					toolbar1: 'formatselect | bold italic | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link',
    					plugins: 'link,lists,textcolor,colorpicker',
    					menubar: false,
    					statusbar: false,
    				},
    				quicktags: true,
    				mediaButtons: false,
    			});
    
    			$('#submit').mousedown( e => {
    				console.debug('submit.mousedown', e);
    				e.preventDefault();
    	
    				tinyMCE.triggerSave();
    	
    				$(e.currentTarget).trigger('click');
    			});
    		});