Hello everyone, here is a code snippet you can throw in a functions.php file or a standalone plugin file (outside of a class). I had this in a namespace so I use a leading slash on WP_CLI. I am offering this under the GPL license and please feel free to roll into the plugin. (WP CLI is a command line interface for your server) https://wp-cli.org/
Offers commands like this so you can manually control these user states (and get back in your site again!) I wrote this because I didn’t want to fiddle around with update_user_meta commands. Actually have never written a WP_CLI command before! Woohoo.
It will also alert you and error out if the UID you attempt to set does not exist.
wp uv-set 5 0 // unverifies UID 5
wp uv-set 1 1 // verifies UID 1
wp uv-check 1 // tells you if UID1 is verified or unverified, and prints the verification key.
if ( defined( 'WP_CLI' ) && \WP_CLI ) {
/**
* Check user-verification state in WP CLI (enter UID)
*/
$check_verification = static function ( $args ): void {
if (!isset ($args) || count($args) === 0) {
\WP_CLI::error( "No user specified.", $exit = true );
}
$user_id = $args[0];
$user_activation_status = get_user_meta($user_id, 'user_activation_status', true);
//$user_activation_status = empty($user_activation_status) ? 0 : $user_activation_status;
$uv_status = $user_activation_status === 1 ? __('Verified', 'user-verification') : __('Unverified', 'user-verification');
$activation_key = get_user_meta($user_id, 'user_activation_key', true);
if ($user_activation_status === '1') {
\WP_CLI::log( 'Verified' );
}
if ($user_activation_status === '0') {
\WP_CLI::log( 'Unverified');
}
if ($user_activation_status === '') {
\WP_CLI::log( 'Old User' );
}
\WP_CLI::log( 'Activation key: ' . $activation_key );
//\WP_CLI::success($args[0]);
};
\WP_CLI::add_command( 'uv-check', $check_verification );
/**
* Manual verify: UID + 0/1 . Set to Verified (0) or unverified (1)
*/
$set_verification = static function ( $args ) {
if (!isset ($args) || count($args) === 0) {
\WP_CLI::error( "No user specified.", $exit = true );
}
if ( count($args) === 1 || count($args) >= 3) {
\WP_CLI::error( "Wrong number of arguments.", $exit = true );
}
$user_id = $args[0];
$new_status = $args[1];
$user = get_userdata( $user_id );
if ( $user === false ) {
\WP_CLI::error( "User not found. (UID: " . $user_id . ")", $exit = true );
}
try {
update_user_meta($user_id, 'user_activation_status', $new_status);
\WP_CLI::success('User (UID '. $user_id .') changed: ' . $new_status);
} catch ( \Exception $e ) {
\WP_CLI::error( $e->getMessage(), $exit = true );
}
};
\WP_CLI::add_command( 'uv-set', $set_verification );
}
-
This reply was modified 2 months, 1 week ago by
hongpong.