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

How to use delete_term action in WordPress

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

So, you’re diving into WordPress hooks, huh? Great choice! Today, let’s talk about the delete_term action. This hook can be super handy when you need to perform specific actions whenever a term is deleted in WordPress. Oh, and by the way, there’s also the delete_$taxonomy hook if you want to target a specific taxonomy.

To get started with the delete_term action, you first need to register it using add_action. You can write this code into your theme’s functions.php file or better yet, create a custom WordPress plugin. WePlugins always prefers creating a custom plugin to ensure nothing breaks when you update your theme in the future.

Here’s a quick rundown of the parameters you’ll need for this hook:

  • $term: (int) Term ID.
  • $tt_id: (int) Term taxonomy ID.
  • $taxonomy: (string) Taxonomy slug.
  • $deleted_term: (WP_Term) Copy of the already-deleted term.
  • $object_ids: (array) List of term object IDs.

Live Example 1: Basic Usage

First up, a straightforward example of how to use this hook.

    function weplugins_execute_on_delete_term_event($term, $tt_id, $taxonomy, $deleted_term, $object_ids) {
        // Code to execute when the term is deleted
    }
    add_action("delete_term", "weplugins_execute_on_delete_term_event", 10, 5);
    

Live Example 2: Logging Deleted Terms

Next, let’s log deleted terms for debugging purposes.

    function weplugins_log_deleted_term($term, $tt_id, $taxonomy, $deleted_term, $object_ids) {
        error_log("Deleted term ID: " . $term);
    }
    add_action("delete_term", "weplugins_log_deleted_term", 10, 5);
    

Live Example 3: Removing the Hook

Sometimes, you need to remove a registered hook. Here’s how to do that:

    remove_action("delete_term", "weplugins_execute_on_delete_term_event", 10, 5);
    

Make sure you provide the same callback function name, priority, and number of arguments while removing the hook callback.

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

Access Premium WordPress Plugins

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.