Using WordPress ‘add_category_form_pre’ PHP action

The add_category_form_pre WordPress PHP action fires before the Add Category form is displayed.

Usage

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

Parameters

  • $arg (object) – The arguments cast to an object.

More information

See WordPress Developer Resources: add_category_form_pre

Examples

Add a custom field to the Add Category form

This example adds a custom field to the Add Category form for storing extra information.

add_action('add_category_form_pre', 'add_custom_field_to_category_form');
function add_custom_field_to_category_form($arg) {
    ?>
    <div class="form-field">
        <label for="extra_info">Extra Info</label>
        <input name="extra_info" id="extra_info" type="text" />
        <p class="description">Enter some extra information for this category.</p>
    </div>
    <?php
}

Add a custom notice before the Add Category form

This example adds a custom notice at the beginning of the Add Category form.

add_action('add_category_form_pre', 'add_custom_notice_to_category_form');
function add_custom_notice_to_category_form($arg) {
    ?>
    <div class="notice notice-info">
        <p>Please make sure to fill out all fields before submitting the category.</p>
    </div>
    <?php
}

Modify the Add Category form title

This example modifies the title of the Add Category form.

add_action('add_category_form_pre', 'modify_category_form_title');
function modify_category_form_title($arg) {
    ?>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            var title = document.querySelector('#addtag h2');
            if (title) {
                title.textContent = 'Add a New Custom Category';
            }
        });
    </script>
    <?php
}

Add a custom CSS class to the Add Category form

This example adds a custom CSS class to the Add Category form.

add_action('add_category_form_pre', 'add_custom_class_to_category_form');
function add_custom_class_to_category_form($arg) {
    ?>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            var form = document.querySelector('#addtag');
            if (form) {
                form.classList.add('custom-category-form');
            }
        });
    </script>
    <?php
}

Add a custom validation to the Add Category form

This example adds custom validation to the Add Category form to ensure the category name is not “Uncategorized.”

add_action('add_category_form_pre', 'add_custom_validation_to_category_form');
function add_custom_validation_to_category_form($arg) {
    ?>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            var form = document.querySelector('#addtag');
            if (form) {
                form.addEventListener('submit', function(event) {
                    var categoryName = document.querySelector('#tag-name');
                    if (categoryName.value.toLowerCase() === 'uncategorized') {
                        event.preventDefault();
                        alert('Please enter a different category name.');
                    }
                });
            }
        });
    </script>
    <?php
}