Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Created April 29, 2021 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JarrydLong/e4567ad77d001f60336a1edffd6fc596 to your computer and use it in GitHub Desktop.
Save JarrydLong/e4567ad77d001f60336a1edffd6fc596 to your computer and use it in GitHub Desktop.
<?php
/**
* This will remove all members who have an active membership and have bought through PayPal
* or PayPal Express
*
* Exercise caution when making these changes. They cannot be undone. Make a backup of your database
* before running this script.
*
* Enter /wp-admin/?removepaypal=true in your URL to run the script.
*
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function mypmpro_remove_paypal_users(){
if( isset( $_GET['removepaypal'] ) ){
global $wpdb;
//Get all transactions that were purchased using PayPal or PayPal Express
$sql = "SELECT * FROM $wpdb->pmpro_membership_orders WHERE `gateway` = 'paypal' OR `gateway` = 'paypalexpress'";
$results = $wpdb->get_results( $sql );
if( $results ){
foreach( $results as $result ){
//Lets make sure they're still using PayPal and haven't signed up with a different gateway
$last_order = new MemberOrder();
$last_order->getLastMemberOrder( $result->user_id, 'success' );
if( $last_order->gateway === 'paypal' || $last_order === 'paypalexpress' ){
//They're still using PayPal - Remove their membership
$removed = pmpro_changeMembershipLevel( 0, $result->user_id );
echo 'Removed Membership - '.$removed.' - '.$result->user_id;
}
}
}
exit( 'End' );
}
}
add_action( 'admin_init', 'mypmpro_remove_paypal_users' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment