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

How to use deleted_term_relationships action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
January 23, 2023
5 minutes read

deleted_term_relationships action

Fires immediately after an object-term relationship is deleted.

To use the deleted_term_relationships 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.

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_deleted_term_relationships_event which takes 3 parameters and we registered using add_action. The first parameter deleted_term_relationships is the name of the hook, The second parameter execute_on_deleted_term_relationships_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 deleted_term_relationships action.

Parameters

    Below are the 3 parameters required to use this hook.

  • $object_id: (int) Object ID.
  • $tt_ids: (array) An array of term taxonomy IDs.
  • $taxonomy: (string) Taxonomy slug.

Live Example 1

Below is a basic example of how you can use this hook.

    function weplugins_execute_on_deleted_term_relationships_event($object_id, $tt_ids, $taxonomy){
       // Code to be executed when this action occurs in WordPress.
    }
    // Add the action
    add_action("deleted_term_relationships", "weplugins_execute_on_deleted_term_relationships_event", 10, 3);
    

Live Example 2

This example demonstrates logging the deleted term relationships into a custom log file.

    function weplugins_log_deleted_term_relationships($object_id, $tt_ids, $taxonomy){
       $log = "Object ID: $object_id, Term Taxonomy IDs: " . implode(',', $tt_ids) . ", Taxonomy: $taxonomy" . PHP_EOL;
       file_put_contents(ABSPATH . 'wp-content/deleted_term_relationships.log', $log, FILE_APPEND);
    }
    // Add the action
    add_action("deleted_term_relationships", "weplugins_log_deleted_term_relationships", 10, 3);
    

Live Example 3

In this example, we send an email notification when a term relationship is deleted.

    function weplugins_notify_deleted_term_relationships($object_id, $tt_ids, $taxonomy){
       $to = 'admin@example.com';
       $subject = 'Term Relationship Deleted';
       $message = "A term relationship has been deleted.nnObject ID: $object_idnTerm Taxonomy IDs: " . implode(',', $tt_ids) . "nTaxonomy: $taxonomy";
       wp_mail($to, $subject, $message);
    }
    // Add the action
    add_action("deleted_term_relationships", "weplugins_notify_deleted_term_relationships", 10, 3);
    

Removing the Hook

To remove a hook callback, use the example below.

    remove_action("deleted_term_relationships", "weplugins_execute_on_deleted_term_relationships_event", 10, 3);
    

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

Access Premium WordPress Plugins

Contact Us

If you need any customization or assistance using this hook, feel free to contact us. We’re here 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.