Title: Hook function prints output twice
Last modified: May 3, 2017

---

# Hook function prints output twice

 *  [Subrata Sarkar](https://wordpress.org/support/users/subrataemfluence/)
 * (@subrataemfluence)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/hook-function-prints-output-twice/)
 * Hello everybody,
    I have started learning Hooks today and trying to accomplish
   the following.
 * When the home page of my site loads, it inserts a new term in my custom taxonomy
   called “Amenities”. Then using a dynamic Hook (`create_{$taxonomy}`, in my case
   it is `create_amenities`) I am printing the newly added `term_id`.
 * This is what I did.
 * **1. Created a function to print the new term_id, which accepts one parameter
   and hooked it into above.**
 *     ```
       add_action('create_amenities', 'tourplan_print_new_amenity_term_id', 10, 1);
   
       function tourplan_print_new_amenity_term_id($term_id) {
          // Printing twice instead of only once.
          echo 'The newly added term id is: ' . $term_id;
       }
       ```
   
 * **2. Inside my index.php I have written the following:**
 *     ```
       $arr_term = array(
         'description' => 'Honeymoon Suit for couple',
         'slug'        => 'honeymoon-suite'
       )
   
       $new_term = $wp_insert_term('Honeymoon Suit', 'amenities', $arr_term);
       $new_term_id = $new_term['term_id'];
   
       do_action('create_amenities', $new_term_id);
       ```
   
 * **3. And finally I am removing the term from taxonomy after calling above `do_action`
   method:**
 * `wp_remove_term($new_term_id, 'amenities');`
 * Everything works fine except the Hook function is printing value of ID twice.
   What I am doing wrong?
 * PS: I am not using any ready made theme or any child theme. It is a completely
   hand-built theme from scratch.
 * Regards,
    Subrata

Viewing 1 replies (of 1 total)

 *  [karimeo](https://wordpress.org/support/users/karimeo/)
 * (@karimeo)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/hook-function-prints-output-twice/#post-9095059)
 * Hi Subrata,
 * The reason why the function attached to the Hook is called twice, is because 
   the Hook is actually executed twice 🙂
 * You don’t need to call to do_action(). do_action( ‘create_amenities’, … ) is 
   automatically called by WordPress in the wp_insert_term() function.
 * You would only use do_action() if you want to create your own Hook. In this case,
   you’re not creating a Hook. You’re attaching to an existing Hook offered by WordPress.
 * I hope this helps you.
 * Regards,
    Karim.

Viewing 1 replies (of 1 total)

The topic ‘Hook function prints output twice’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [karimeo](https://wordpress.org/support/users/karimeo/)
 * Last activity: [9 years, 1 month ago](https://wordpress.org/support/topic/hook-function-prints-output-twice/#post-9095059)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
