Skip to content

Instantly share code, notes, and snippets.

@asadowski10

asadowski10/tools.php

Last active Oct 14, 2016
Embed
What would you like to do?
Remove all WordPress Customers - CLI Tools
<?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