Using WordPress ‘add_tag_form_pre’ PHP action

The add_tag_form_pre WordPress PHP action fires before the Add Tag form, allowing you to add custom code before the form is displayed.

Usage

add_action('add_tag_form_pre', 'your_custom_function');
function your_custom_function($taxonomy) {
    // your custom code here
}

Parameters

  • $taxonomy (string) – The taxonomy slug.

More information

See WordPress Developer Resources: add_tag_form_pre

Examples

Add a custom message above the Add Tag form

Add a custom message above the Add Tag form for a specific taxonomy.

add_action('add_tag_form_pre', 'add_custom_message');
function add_custom_message($taxonomy) {
    if ($taxonomy == 'custom_taxonomy') {
        echo '<p><strong>Reminder:</strong> Please use relevant tags for better organization.</p>';
    }
}

Add a custom field to the Add Tag form

Add a custom field to the Add Tag form for a specific taxonomy.

add_action('add_tag_form_pre', 'add_custom_field');
function add_custom_field($taxonomy) {
    if ($taxonomy == 'custom_taxonomy') {
        echo '<label for="custom_field">Custom Field:</label>';
        echo '<input type="text" name="custom_field" id="custom_field" value="" />';
    }
}

Display a list of existing tags from another taxonomy

Display a list of existing tags from another taxonomy to help users choose appropriate tags.

add_action('add_tag_form_pre', 'display_existing_tags');
function display_existing_tags($taxonomy) {
    if ($taxonomy == 'custom_taxonomy') {
        $tags = get_terms(array('taxonomy' => 'post_tag', 'hide_empty' => false));
        echo '<p>Existing tags in post_tag taxonomy:</p>';
        foreach ($tags as $tag) {
            echo '<span>' . $tag->name . '</span> ';
        }
    }
}

Add custom CSS to style the Add Tag form

Add custom CSS to style the Add Tag form for a specific taxonomy.

add_action('add_tag_form_pre', 'add_custom_css');
function add_custom_css($taxonomy) {
    if ($taxonomy == 'custom_taxonomy') {
        echo '<style>
            .form-wrap {
                background-color: lightblue;
                padding: 20px;
            }
        </style>';
    }
}

Add custom JavaScript to the Add Tag form

Add custom JavaScript to the Add Tag form for a specific taxonomy.

add_action('add_tag_form_pre', 'add_custom_js');
function add_custom_js($taxonomy) {
    if ($taxonomy == 'custom_taxonomy') {
        echo '<script>
            // your custom JavaScript code here
        </script>';
    }
}