2

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.)

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).

5
  • 1
    Where is this code happening? And why would you need to make that include?
    – brasofilo
    Sep 20, 2013 at 10:17
  • 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). Sep 20, 2013 at 10:57
  • The code works for me (without include), I tried inside add_action('admin_head',function(){ /* ALL YOUR CODE */ }); . I'm not sure about those echo and exit that you're using, but they shouldn't prevent the user deletion. I guess it depends on what's happening before and after your code.
    – brasofilo
    Sep 20, 2013 at 12:04
  • Thanks for the info brasofilo! Sorry Im new to wordpress... admin_head puts the code into the head of the admin page doesnt it? Or can it be used differently? Can wpmu_delete_user only be run by an admin so my issue could be a permissions problem? Sep 20, 2013 at 15:40
  • That was only a raw example to test the code and make sure it works (admin_head has no use in your case). Show the rest of your code.
    – brasofilo
    Sep 20, 2013 at 16:10

1 Answer 1

3

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;
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.