Last active
October 14, 2016 12:59
-
-
Save asadowski10/4a6d98b70e55d13ebde67882fbd1a1e3 to your computer and use it in GitHub Desktop.
Remove all WordPress Customers - CLI Tools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( php_sapi_name() !== 'cli' || isset( $_SERVER['REMOTE_ADDR'] ) ) { | |
die( 'CLI Only' ); | |
} | |
// Get first arg | |
if ( ! isset( $argv ) || count( $argv ) < 2 ) { | |
echo "Missing parameters.\n"; | |
echo "script usage: php tools.php [domain]\n"; | |
die(); | |
} | |
//Domain | |
$domain = ( isset( $argv[1] ) ) ? $argv[1] : ''; | |
// Fake WordPress, build server array | |
$_SERVER = array( | |
'HTTP_HOST' => $domain, | |
'SERVER_NAME' => $domain, | |
'REQUEST_URI' => '', | |
'REQUEST_METHOD' => 'GET', | |
'SCRIPT_NAME' => basename( __FILE__ ), | |
'SCRIPT_FILENAME' => basename( __FILE__ ), | |
'PHP_SELF' => basename( __FILE__ ), | |
); | |
@ini_set( 'memory_limit', - 1 ); | |
@ini_set( 'display_errors', 1 ); | |
require( dirname( __FILE__ ) . '/wp-load.php' ); | |
@ini_set( 'memory_limit', - 1 ); | |
@ini_set( 'display_errors', 1 ); | |
global $wpdb; | |
// Remove this line | |
$users_ids = $wpdb->get_results( "SELECT $wpdb->users.ID as user_id FROM $wpdb->users INNER JOIN $wpdb->usermeta ON ( $wpdb->users.ID = $wpdb->usermeta.user_id ) WHERE 1=1 AND ( | |
( | |
( $wpdb->usermeta.meta_key = 'wp_capabilities' AND CAST($wpdb->usermeta.meta_value AS CHAR) LIKE '%\"Customer\"%' ) | |
) | |
)" ); | |
if ( ! empty( $users_ids ) ) { | |
foreach ( $users_ids as $_userid ) { | |
$wpdb->query( "DELETE FROM $wpdb->users WHERE ID = $_userid->user_id" ); | |
} | |
} | |
// remove user meta orphaned | |
$wpdb->query( "DELETE um | |
FROM $wpdb->usermeta um | |
LEFT JOIN $wpdb->users us ON us.ID = um.user_id | |
WHERE us.ID IS NULL" ); | |
die( 'finish' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment