I’m using the following function to create a portfolio category named “tips_username” when a user registers on my WordPress website.
add_action('user_register', 'auto_create_traveltip_category');
function auto_create_traveltip_category($user_id) {
$tip_category_name = 'tips_' . $_POST['user_login'];
wp_insert_term($tip_category_name, 'portfolio_entries', array(
'description' => 'Travel Tip for ' . $_POST['user_login'],
'slug' => 'tips-' . $_POST['user_login'],
'parent' => '',
)
);
}
This is working fine, but now I need a function to automatically delete the category when I delete a user, for example from the user list. I implemented the following function taking inspiration from https://codex.wordpress.org/Plugin_API/Action_Reference/delete_user:
function delete_traveltip_category_with_user($user_id) {
global $wpdb;
$user_obj = get_userdata($user_id);
$tip_cat_name = 'tips_' . $user_obj->user_login;
$tip_cat_desc = get_term_by('name', $tip_cat_name, 'portfolio_entries');
$tip_cat_id = $tip_cat_desc->term_id;
wp_delete_term( $tip_cat_id, 'portfolio_entries' );
}
add_action( 'delete_user', 'delete_traveltip_category_with_user' );
Unfortunately it is not working. Can you please have a look and tell me what’s wrong with it? Thanks!!
It seems the
user_id
argument is not set. You have to call thedo_action()
function to pass theuser_id
value to your function.source : do_action()
EDIT
You have to call the
do_action()
function in your template (delete.php for example).do_action('delete_traveltip_category_with_user', $user_id)
Where $user_id is the id to delete. It will call your custom function from the template.
Hope it helps.