Using WordPress ‘edit_category_form_pre’ PHP action

The edit_category_form_pre WordPress PHP action fires before the Edit Category form, allowing you to modify the form or add custom fields.

Usage

add_action('edit_category_form_pre', 'your_custom_function');
function your_custom_function($tag) {
    // Your custom code here
}

Parameters

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

More information

See WordPress Developer Resources: edit_category_form_pre

Examples

Adding a custom field to the Edit Category form

Add a custom text field to the Edit Category form and display it before the form.

add_action('edit_category_form_pre', 'add_custom_field_to_edit_category');
function add_custom_field_to_edit_category($tag) {
    // Retrieve the value of the custom field
    $custom_field_value = get_term_meta($tag->term_id, 'custom_field', true);
    // Display the custom field
    echo '<label for="custom_field">Custom Field</label>';
    echo '<input type="text" name="custom_field" value="' . esc_attr($custom_field_value) . '">';
}

Modifying the category title

Change the category title to uppercase before displaying it on the Edit Category form.

add_action('edit_category_form_pre', 'uppercase_category_title');
function uppercase_category_title($tag) {
    $tag->name = strtoupper($tag->name);
}

Displaying a custom message

Display a custom message below the category description field in the Edit Category form.

add_action('edit_category_form_pre', 'display_custom_message_below_description');
function display_custom_message_below_description($tag) {
    echo '<p class="description">This is a custom message below the description field.</p>';
}

Adding custom styles to the Edit Category form

Inject custom CSS styles into the Edit Category form.

add_action('edit_category_form_pre', 'add_custom_styles_to_edit_category');
function add_custom_styles_to_edit_category($tag) {
    echo '<style>
        .form-wrap {
            background-color: lightgrey;
        }
    </style>';
}

Prepopulating the category description

Prepopulate the category description field with a default value when the field is empty.

add_action('edit_category_form_pre', 'prepopulate_category_description');
function prepopulate_category_description($tag) {
    if (empty($tag->description)) {
        $tag->description = 'Default category description';
    }
}