• Can someone tell me how I can control/change the redirect of a user who just deleted their own account? I would like to send them to a specific url page.

    Thanks in advance for your help.

Viewing 13 replies - 1 through 13 (of 13 total)
  • You can use the <a href="https://codex.www.ads-software.com/Plugin_API/Action_Reference/delete_user">delete_user</a> action on the codex page you’ll see this example:
    Example: Email Users Being Deleted

    function my_delete_user( $user_id ) {
    	global $wpdb;
    
            $user_obj = get_userdata( $user_id );
            $email = $user_obj->user_email;
    
    	$headers = 'From: ' . get_bloginfo( "name" ) . ' <' . get_bloginfo( "admin_email" ) . '>' . "\r\n";
     	 wp_mail( $email, 'You are being deleted, brah', 'Your account at ' . get_bloginfo("name") . ' is being deleted right now.', $headers );
    }
    add_action( 'delete_user', 'my_delete_user' );

    instead of sending an e-mail to the user check them not to be still logged in (if an admin deletes their email for example) and if they are not logged in redirect them…

    Thread Starter haagsekak

    (@haagsekak)

    Dear jnhghy,

    Thank you for your prompt assistance!

    I think you may have misunderstood when I wrote;

    I would like to send them to a specific url page.

    You mention emails but I was referring to redirecting them back to the home page or to a specific URL upon the user deleting their user account.

    Does your explanation still apply in this case?

    Thank you for help.

    Hi haagsekak,
    Sorry, I’ve understand your issue, I just thought you are looking only for an example on how you can do it… Here is a code that should work:

    function redirect_deleted_user( $user_id ) {
        if ( $user_id != get_current_user_id() ) {
            wp_redirect( 'https://www.example.com', 301 ); exit;
        }
    }
    add_action( 'delete_user', 'redirect_deleted_user' );

    This code goes to your functions.php file ( if you have a child theme ) if you don’t have a child theme, you’ll need one or your changes will get overwritten when you update your theme*).

    *To create a child theme you can use a plugin like Child Theme Creator by Orbisius (there are other plugins that are qutie similar with this one – here is a list

    So after you have the child theme, add the code to its functions.php file

    if you don’t want to create a child theme for this you can also add it as a plugin:

    for that create a file with the fallowing content (in notepad):

    <?php
    /**
     * Plugin Name: Deleted User Redirect
     * Plugin URI: https://URI_Of_Page_Describing_Plugin_and_Updates
     * Description: This plugin adds functionality to the blog, it redirects the user to a specific hard coded url when he/she deletes hes/hers account.
     * Version: 1.0.0
     * Author: Name of the plugin author
     * Author URI: https://URI_Of_The_Plugin_Author
     * Text Domain: redirect-deleted-user
     * License: GPL2
     */
    
    defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    function redirect_deleted_user( $user_id ) {
        if ( $user_id != get_current_user_id() ) {
            wp_redirect( 'https://www.example.com', 301 ); exit;
        }
    }
    add_action( 'delete_user', 'redirect_deleted_user' );

    Save this file as redirect-deleted-user.php in a folder named redirect-deleted-user create a zip of that folder. Go to your website backend to Plugins->add new plugin -> upload plugin and upload the new zip file then activate this plugin and everything should work.

    You have 2 ways of achieving this. In both cases the page the user is redirected to is hard coded in the script.

    You can create an options page for your plugin if you want to be able to change the page the user gets redirected when he/she delelets his/hers account throw options in backend, just check the codex for this.

    If you have other questions or you get errors or anything else please ask away and excuse my first answer I just thought it was enough for you to build a solution.

    Now I’ve found this plugin also: Delete Me that might work for you, please check it to see if it has what you need.

    Thread Starter haagsekak

    (@haagsekak)

    Thank you jnhghy, I will review your answers. FYI I do have a child theme so I will try your suggestions. Try to minimize plugin usage due to infrequent and often delayed updates to current compatibility requirements.
    Thanks again. I will reply with outcome shortly.

    Thread Starter haagsekak

    (@haagsekak)

    @jnhghy I tried your function hack and it’s not working for me, I changed the quotation from single to double (looked at a redirect I have for logging out) but that also didn’t work. Below the code I inserted.

    function redirect_deleted_user( $user_id ) {
        if ( $user_id != get_current_user_id() ) {
            wp_redirect( "https://vatpt1.thevillageblog.com/home/", 301 ); exit;
        }
    }
    add_action( 'delete_user', 'redirect_deleted_user' );

    All I did was to put it in my /child-theme/functions.php. Did I miss a step?

    Oh I’m very very sorry, I’ve wanted to make sure that if an admin is deleteing an user he deson’t get redirected but instead of doing that I’ve created a bug :(.
    Please try this code for now and see if it works:

    function redirect_deleted_user( $user_id ) {
        wp_redirect( "https://vatpt1.thevillageblog.com/home/", 301 ); exit;
    }
    add_action( 'delete_user', 'redirect_deleted_user' );

    Thread Starter haagsekak

    (@haagsekak)

    Awesome! that worked like a charm. Thank you for your help.

    I’m happy to hear it’s working, sorry for the bug in the first code… onest mistake ??

    Thread Starter haagsekak

    (@haagsekak)

    Dear jnhghy,
    I guess I missed a step and when I tested again as an admin i was redirected out of the site. I was deleting a spam user in admin panel > users > pending (status), then clicked on delete and was instantly sent to my redirect page.

    below is the code i used.

    function redirect_deleted_user( $user_id ) {
        wp_redirect( "https://vatpt1.thevillageblog.com/home/", 301 ); exit;
    }
    add_action( 'delete_user', 'redirect_deleted_user' );

    any thoughts?

    Please try this code (untested):

    function redirect_deleted_user( $user_id ) {
        if ( $user_id == get_current_user_id() ) {
            wp_redirect( 'https://www.example.com', 301 ); exit;
        }
    }
    add_action( 'delete_user', 'redirect_deleted_user' );

    Thread Starter haagsekak

    (@haagsekak)

    Hi @jnhghy,

    function redirect_deleted_user( $user_id ) {
        if ( $user_id == get_current_user_id() ) {
            wp_redirect( 'https://www.example.com', 301 ); exit;
        }
    }
    add_action( 'delete_user', 'redirect_deleted_user' );

    Tried this code and it still logs out admin after deleting user.
    Somehow the only thing missing is the recognition that administrator is the user who is deleting and not an author or other non-admin. I am sure there is a way. I wish I was more savvy in code so I could be of more assistance. Please keep trying if you have time.
    Many thanks!

    Thread Starter haagsekak

    (@haagsekak)

    Hi @jnhghy,

    Hope your are still around?

    I migrated to the latest WP and BP, using the twentysixteen theme.

    I noticed that when I deleted a test user by going to profile settings and deleting account it only did a partial delete of account information. So I removed your redirect and now it deleted the entire account but puts the user at the front page of the site which makes sense, but I still want the user to go back to the wp-login.php.

    Is there another hack that can do this? Maybe the new code doesn’t make your code work properly anymore. That said I don’t remember if I did the same test before so this could have been an existing issue gone undetected.

    Anyway hope you are still willing to help.

    Thanks in advance!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘How to redirect a user who just deleted their account?’ is closed to new replies.