Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use add_tag_form_fields action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 18, 2022
5 minutes read

add_tag_form_fields action

Fires after the Add Tag form fields for non-hierarchical taxonomies.

To use the add_tag_form_fields action, first you have to register it using add_action. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin.

At WePlugins, we always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function weplugins_execute_on_add_tag_form_fields_event which takes 1 parameter and we registered it using add_action. The first parameter add_tag_form_fields is the name of the hook, the second parameter weplugins_execute_on_add_tag_form_fields_event is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook so you can use remove_action to remove the add_tag_form_fields action.

Parameters

Below the 1 parameter is required to use this hook.

  • $taxonomy : (string) The taxonomy slug.

Live Example

do_action('add_tag_form_fields', string $taxonomy)

Below is an example of how you can use this hook.

Example 1: Adding a Custom Field

This example shows how to add a custom field in the Add Tag form.

    function weplugins_execute_on_add_tag_form_fields_event($taxonomy) {
        // Add custom field here
        ?>
        <div class="form-field term-group">
            <label for="weplugins_custom_field"><?php _e('Custom Field', 'weplugins'); ?></label>
            <input type="text" id="weplugins_custom_field" name="weplugins_custom_field" value="">
        </div>
        <?php
    }
    add_action('add_tag_form_fields', 'weplugins_execute_on_add_tag_form_fields_event', 10, 1);
    

Example 2: Handling Form Submission

This example demonstrates how to handle the form submission of the custom field added in the Add Tag form.

    function weplugins_save_custom_field($term_id) {
        if (isset($_POST['weplugins_custom_field'])) {
            add_term_meta($term_id, 'weplugins_custom_field', sanitize_text_field($_POST['weplugins_custom_field']));
        }
    }
    add_action('created_term', 'weplugins_save_custom_field', 10, 2);
    

Example 3: Removing the Custom Field

This example explains how to remove the custom field from the Add Tag form.

    remove_action('add_tag_form_fields', 'weplugins_execute_on_add_tag_form_fields_event', 10, 1);
    

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook, please contact our team and we’d be happy to assist you.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.