I have started learning Hooks today and trying to accomplish the following.
When the home page of my site loads, it would insert a new term in my custom taxonomy called amenities
. Then using a dynamic Hook (create_{$taxonomy}
, in my case it is create_amenities
) and with the help of a custom function hooked into 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 while should only print 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 the value of ID twice. What am I doing wrong?
PS: I am not using any ready made theme or any child theme. It is a completely hand-built theme from scratch.