Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JarrydLong/fe92ed1dc28bccf2141ac4a025b850de to your computer and use it in GitHub Desktop.
Save JarrydLong/fe92ed1dc28bccf2141ac4a025b850de to your computer and use it in GitHub Desktop.
Removes a user's membership and order data either by manually adding in a query param, or when deleting a user through /wp-admin/
<?php
/**
* Remove user data manually by adjusting the ID's in the array, or adding them into a
* query param by setting it to /?reset_test_accounts=true&accounts=2,3
* This works well if you've deleted the user accounts already and want to remove the
* first few users you'd initially tested with
*/
function pmpro_remove_test_accounts( $user_id = null ){
if( isset( $_REQUEST['reset_test_accounts'] ) && $_REQUEST['reset_test_accounts'] == 'true' ){
global $wpdb;
// Change this according to the user's you'd like to remove
$user_ids = array( 2, 3 );
// Or set it in the URL /?reset_test_accounts=true&accounts=2,3
if( isset( $_REQUEST['accounts'] ) )
$user_str = $_REQUEST['accounts'];
$user_ids = explode( ",", $user_str );
if( is_array( $user_ids ) ){
foreach( $user_ids as $uid ){
//Deleting the member details as well as the order
$wpdb->delete( $wpdb->pmpro_memberships_users, array( 'user_id' => intval( $uid ) ) );
$wpdb->delete( $wpdb->pmpro_membership_orders, array( 'user_id' => intval( $uid ) ) );
}
}
//Results are cached - this will force a new query to be run with the updated results
delete_transient( 'pmpro_report_memberships_signups' );
delete_transient( 'pmpro_report_memberships_cancellations' );
}
}
add_action( 'init', 'pmpro_remove_test_accounts' );
/**
* Removes the user's membership and order data when deleting the user
* from the 'Users' page.
*/
function pmpro_remove_test_accounts_on_delete( $user_id ){
global $wpdb;
//Deleting the member details as well as the order
$wpdb->delete( $wpdb->pmpro_memberships_users, array( 'user_id' => intval( $user_id ) ) );
$wpdb->delete( $wpdb->pmpro_membership_orders, array( 'user_id' => intval( $user_id ) ) );
//Results are cached - this will force a new query to be run with the updated results
delete_transient( 'pmpro_report_memberships_signups' );
delete_transient( 'pmpro_report_memberships_cancellations' );
}
add_action( 'delete_user', 'pmpro_remove_test_accounts_on_delete', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment