Deleting a user in multisite

I am trying to delete a user when a payment gateway requests, I have the following code:

(Edited code to show full function that communicates with the payment gateway, the echo’s give the response to the payment gateway the exit’s are because it’s used in the payment gateways example code.)

Read More
function handle_gateway_return() {
    $GWPass = get_option( $this->gateway . "_gateway_gwpass" );

    if ($_GET["GWPass"] != $GWPass ) {
        header("HTTP/1.0 401 Unauthorized");
        echo "<h1>Gateway 1.1</h1><h3>Authentication failed.</h3>";
        exit;
    }

    //==================================================
    // Action: user.add
    //==================================================

    if ($_GET["Action"] == "user.add") {
        // Load variables
        $ZFirstName     = trim($_GET['FIRSTNAME']);
        $ZLastName      = trim($_GET['LASTNAME']);
        $ZFullName      = $ZFirstName." ".$ZLastName;
        $ZUserName      = trim($_GET['username']);
        $ZEmail         = trim($_GET['EMAIL']);
        $ZPassword      = trim($_GET['password']); 
        $ZPassword      = md5($ZPassword); //md5

    if ( username_exists( $ZUserName ) ) {
        echo "OK|User Added!";
        exit;   
        }

    if ( !username_exists( $ZUserName ) ) {
        wp_create_user( $ZUserName, $ZPassword, $ZEmail );
        echo "OK|User Added!";
        exit;
        }

    }

    //==================================================
    // Action: user.delete
    //==================================================

    else if ($_GET["Action"] == "user.delete") {
        $ZUserName = trim($_GET['username']);

    // Grab user_id
    $user = get_user_by('login', $ZUserName);

    if (!$user) {
        echo 'USER_DOES_NOT_EXIST';
        exit;
        }

    $url = network_site_url('/wp-admin/includes/ms.php');
    if ($user) {
        include($url);
        wpmu_delete_user($user->ID);
        echo "OK|User Deleted!";
        exit;
        }

    } else { 
        echo "UNKNOWN_ACTION|UNKNOWN_ACTION";
        exit;
    }   

}

However the user does not get deleted! What am I doing wrong?

Thanks in advance for your help!

Edit: This is happening within a payment gateway addon file used in wpmudevs membership plugin. In the addon file I have a function that deals with the gateway return and creating the user works fine using wp_create_user and yes there was no need for an include there. I added the include here simply because I saw it mentioned somewhere and thought I would give it a go as without the include did not work either (but I didn’t think it was needed).

Related posts

1 comment

  1. I ended up figuring this out a while ago… completely forgot to post it here.

    Once I added the include properly it worked:

    require_once('./wp-admin/includes/ms.php');
       if ( $user ) {
        wpmu_delete_user( $user->ID );
        }
    

    For the sake of future readers here was the final code:

    function handle_gateway_return() {
    
        $GWPass = get_option( $this->gateway . "_gateway_gwpass" );
    
        if ($_GET["GWPass"] != $GWPass) {     
    
        header("HTTP/1.0 401 Unauthorized");
    
        echo "<h1>Gateway 1.1</h1><h3>Authentication failed.</h3>";
    
        exit;  
    
        }
    
    
        if ($_GET["Action"] == "user.add") {    
    
        // Load variables.
    
        $ZFirstName        = trim($_GET['FIRSTNAME']);
    
        $ZLastName      = trim($_GET['LASTNAME']);
    
        $ZFullName      = $ZFirstName." ".$ZLastName;
    
        $ZUserName      = trim($_GET['username']);
    
        $ZEmail         = trim($_GET['EMAIL']);
    
        $ZPassword      = trim($_GET['password']); 
    
        $ZPassword      = md5($ZPassword); //md5
    
        if ( !username_exists( $ZUserName ) ) {
            wp_create_user( $ZUserName, $ZPassword, $ZEmail );
        }   
    
        // Tell server that user was added
    
        echo "OK|User added!";
         exit;
          } 
    
        else if ($_GET["Action"] == "user.delete") { 
    
            $ZUserName = trim($_GET['username']);
    
            $user = get_user_by( 'login', $ZUserName );
    
            require_once('./wp-admin/includes/ms.php');
        if ( $user ) {
        wpmu_delete_user( $user->ID );
        } else {
            echo "User Not Deleted!";
            exit;
        }
    
                // Tell server user deleted
        echo "OK|User deleted!";
        exit;
        }
    
        else { 
    
        echo "UNKNOW_ACTION|UNKNOW_ACTION";
        exit;
        }
      }
    

Comments are closed.