Created
October 2, 2024 19:45
-
-
Save pierrerocket/1765e3241f327910d1fccf2000e5de97 to your computer and use it in GitHub Desktop.
WP Cli Command - Remove Spam Users with Identical First and Last Names
This file contains 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
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
/** | |
* Class User_Cleanup_Command | |
* | |
* Custom WP-CLI command to clean up users by comparing their first and last names. | |
*/ | |
class User_Cleanup_Command { | |
/** | |
* Executes the user cleanup command. | |
* | |
* Iterates over all users, compares their first and last names, and deletes users | |
* whose first and last names are identical. Outputs the status for each user. | |
* | |
* ## EXAMPLES | |
* | |
* wp user_cleanup | |
* | |
* @param array $args Positional arguments (not used). | |
* @param array $assoc_args Associative arguments (not used). | |
*/ | |
public function __invoke( $args, $assoc_args ) { | |
// Include necessary files for user deletion. | |
require_once ABSPATH . 'wp-admin/includes/user.php'; | |
// Get all users. | |
$users = get_users( array( | |
'fields' => array( 'ID', 'user_login', 'user_email' ), | |
) ); | |
foreach ( $users as $user ) { | |
$user_id = $user->ID; | |
$user_login = $user->user_login; | |
$user_email = $user->user_email; | |
// Get first name and last name. | |
$first_name = get_user_meta( $user_id, 'first_name', true ); | |
$last_name = get_user_meta( $user_id, 'last_name', true ); | |
if ( $first_name === $last_name ) { | |
// Delete the user if first and last names are identical. | |
wp_delete_user( $user_id ); | |
WP_CLI::line( "Invalid user - $user_id - $user_login - $user_email" ); | |
} else { | |
// Output if the user is valid. | |
WP_CLI::line( "Valid User" ); | |
} | |
} | |
} | |
} | |
// Register the command with WP-CLI. | |
WP_CLI::add_command( 'user_cleanup', 'User_Cleanup_Command' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment