Using WordPress ‘edit_category_form_fields’ PHP action

The edit_category_form_fields WordPress PHP action is triggered after the Edit Category form fields are displayed, allowing you to add custom fields or modify the existing form.

Note: This hook has been deprecated. Use ‘{$taxonomy_edit_form_fields’} instead.

Usage

add_action('edit_category_form_fields', 'my_custom_function');
function my_custom_function($tag) {
    // your custom code here

    return $tag;
}

Parameters

  • $tag (WP_Term): The current category term object.

More information

See WordPress Developer Resources: edit_category_form_fields

Examples

Add a custom field to the Edit Category form

This example adds a custom field called ‘Category Color’ to the Edit Category form.

add_action('edit_category_form_fields', 'add_category_color_field');
function add_category_color_field($tag) {
    $color = get_term_meta($tag->term_id, 'category_color', true);
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="category_color">Category Color</label></th>
        <td>
            <input type="color" name="category_color" id="category_color" value="<?php echo esc_attr($color); ?>" />
            <p class="description">Choose a color for this category.</p>
        </td>
    </tr>
    <?php
}

Save the custom field value when the Edit Category form is submitted

This example saves the ‘Category Color’ value when the form is submitted.

add_action('edited_category', 'save_category_color_field', 10, 2);
function save_category_color_field($term_id, $tt_id) {
    if (isset($_POST['category_color'])) {
        update_term_meta($term_id, 'category_color', sanitize_hex_color($_POST['category_color']));
    }
}

Add a custom checkbox field to the Edit Category form

This example adds a custom checkbox field called ‘Featured Category’ to the Edit Category form.

add_action('edit_category_form_fields', 'add_featured_category_field');
function add_featured_category_field($tag) {
    $featured = get_term_meta($tag->term_id, 'featured_category', true);
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="featured_category">Featured Category</label></th>
        <td>
            <input type="checkbox" name="featured_category" id="featured_category" <?php checked($featured, '1'); ?> value="1" />
            <span class="description">Mark this category as featured.</span>
        </td>
    </tr>
    <?php
}

Save the custom checkbox field value when the Edit Category form is submitted

This example saves the ‘Featured Category’ value when the form is submitted.

add_action('edited_category', 'save_featured_category_field', 10, 2);
function save_featured_category_field($term_id, $tt_id) {
    if (isset($_POST['featured_category'])) {
        update_term_meta($term_id, 'featured_category', intval($_POST['featured_category']));
    } else {
        delete_term_meta($term_id, 'featured_category');
    }
}