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

How to use create_term action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
October 27, 2022
5 minutes read

create_term action

The create_$taxonomy hook is also available for targeting a specific taxonomy.

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

We at WePlugins 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 execute_on_create_term_event which takes 3 parameters and we registered using add_action. The first parameter create_term is the name of the hook, The second parameter execute_on_create_term_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 create_term action.

Parameters

Below are the 3 parameters required to use this hook:

  • $term_id: (int) Term ID.
  • $tt_id: (int) Term taxonomy ID.
  • $taxonomy: (string) Taxonomy slug.

Live Example

do_action( 'create_term', int $term_id, int $tt_id, string $taxonomy )

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

    function weplugins_execute_on_create_term_event($term_id, $tt_id, $taxonomy){
       //You can write code here to be executed when this action occurs in WordPress. Use the parameters received in the function arguments & implement the required additional custom functionality according to your website requirements.
    }
    // add the action
    add_action( "create_term", "weplugins_execute_on_create_term_event" , 10, 3);

Example 1: Logging Term Creation

In this example, we log the creation of a term to a custom log file.

    function weplugins_log_term_creation($term_id, $tt_id, $taxonomy) {
        $message = "Term ID: $term_id, Taxonomy ID: $tt_id, Taxonomy: $taxonomy";
        error_log($message, 3, WP_CONTENT_DIR . '/term_creation.log');
    }
    add_action("create_term", "weplugins_log_term_creation", 10, 3);
    

Example 2: Send Notification on Term Creation

This example demonstrates sending an email notification whenever a new term is created.

    function weplugins_notify_term_creation($term_id, $tt_id, $taxonomy) {
        $admin_email = get_option('admin_email');
        $subject = "New Term Created";
        $message = "A new term has been created. Term ID: $term_id, Taxonomy: $taxonomy";
        wp_mail($admin_email, $subject, $message);
    }
    add_action("create_term", "weplugins_notify_term_creation", 10, 3);
    

Example 3: Update Term Meta on Creation

Here, we automatically add a custom meta field to the term when it’s created.

    function weplugins_add_term_meta_on_create($term_id, $tt_id, $taxonomy) {
        if ($taxonomy === 'category') {
            add_term_meta($term_id, 'custom_meta_key', 'default_value');
        }
    }
    add_action("create_term", "weplugins_add_term_meta_on_create", 10, 3);
    

Removing the Hook

To remove a hook callback, use the example below.

remove_action( "create_term", "weplugins_execute_on_create_term_event", 10, 3 );

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 need any customization or assistance with this hook, please contact our WordPress Development Team. We’d be happy to help!

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.