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 iscreate_amenities
) I am printing the newly addedterm_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
- The topic ‘Hook function prints output twice’ is closed to new replies.