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

How to use added_term_relationship action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
June 21, 2023
5 minutes read

So you’re diving into the world of WordPress hooks, eh? It’s like adding extra masala to your WordPress site, just the way we like our food with a bit of extra spice! Today, we’re going to talk about the added_term_relationship action. This hook fires right after an object-term relationship is added. It’s a neat way to add functionality without touching the core files. Let’s take a closer look at how we can use this hook effectively.

Example 1: Basic Hook Registration

In this example, we’re registering the added_term_relationship action using add_action. It’s like telling WordPress, “Hey, when this event happens, run this function.”

    function weplugins_execute_on_added_term_relationship_event($object_id, $tt_id, $taxonomy){
       // Code to be executed when the event occurs
    }
    // add the action
    add_action( "added_term_relationship", "weplugins_execute_on_added_term_relationship_event", 10, 3);
    

Example 2: Removing a Hook

Sometimes, you might need to remove a hook. It’s like saying, “I’m done with this task, no need to run it anymore.”

    remove_action( "added_term_relationship", "weplugins_execute_on_added_term_relationship_event", 10, 3 );
    

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

Example 3: Custom Functionality

Here’s how you can implement custom functionality using the parameters received by the hook.

    function weplugins_custom_functionality_on_added_term_relationship($object_id, $tt_id, $taxonomy){
       // Custom code here using $object_id, $tt_id, and $taxonomy
    }
    add_action( "added_term_relationship", "weplugins_custom_functionality_on_added_term_relationship", 10, 3);
    

The parameters for this hook include:

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

Access Premium WordPress Plugins

Need help customizing your WordPress site? Contact Us for expert assistance!

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.