2
votes

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!!

1
Why don't you use this ? <?php wp_delete_category( $cat_ID ) ?> Source : codex.wordpress.org/Function_Reference/wp_delete_categoryJazZ
Thanks for your suggestion Adrien! I tried to use wp_delete_category( $cat_ID ) but it's still not working. I tried to echo the variable $tip_cat_name but I get only "tips_" so there must be something wrong in the first part of the code.Claudio Sinopoli
Have you check $user_id, by echo them , i think you may not get that.Mukesh Ram

1 Answers

0
votes

It seems the user_id argument is not set. You have to call the do_action() function to pass the user_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.