Skip to content

Instantly share code, notes, and snippets.

@guoxiangke
Last active August 29, 2015 14:05
Show Gist options
  • Save guoxiangke/560326fb09110cc7cc81 to your computer and use it in GitHub Desktop.
Save guoxiangke/560326fb09110cc7cc81 to your computer and use it in GitHub Desktop.
wordpress 邮箱验证前禁止注册用户登陆
<?php
/**
* 需求:User can register using email and user name, then log in later after receiving the validation email. All the users share the same avatar.
* 即 邮箱验证前禁止注册用户登陆
* 本需求可以使用Pie Register的验证link 并参照 http://wordpress.org/plugins/wp-members/ 的 wp-members-core.php 代码 进行实现
* Step 1: Enable plugin Pie Register http://wordpress.org/plugins/pie-register/
* Step 2: Go Pie Register -> General Settings (wp-admin/admin.php?page=pie-general-settings) set User Verification => Email Verification.
* Step 3: Add flow code in your theme functions.php
*/
/** initialize **/
add_action( 'after_setup_theme', 'dale_wp_user_login_after_email_validation', 10 );
function dale_wp_user_login_after_email_validation()
{
add_filter( 'authenticate', 'dale_wp_user_login_check_activated', 99, 3 );
}
if( ! function_exists( 'dale_wp_user_login_check_activated' ) ):
/**
* Checks if a user is activated
*
* @since 2.7.1
*
* @param int $user
* @param string $username
* @param string $password
* @uses wp_check_password
* @return int $user
*/
function dale_wp_user_login_check_activated( $user, $username, $password )
{
// password must be validated
$pass = ( ( ! is_wp_error( $user ) ) && $password ) ? wp_check_password( $password, $user->user_pass, $user->ID ) : false;
if( ! $pass ) {
return $user;
}
// activation flag must be validated
$active = get_user_meta( $user->ID, 'active', 1 );
if( $active != 1 ) {
return new WP_Error( 'authentication_failed', __( '<strong>ERROR</strong>: User has not been activated.', 'your_theme_translate_domain' ) );
}
// if the user is validated, return the $user object
return $user;
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment