Wordpress 附加登录规则

Wordpress additional login rules

我做了一个插件,要求用户验证他们的电子邮件地址。它为用户创建带有随机字符串的 "activation_key" 元密钥,并在用户验证后将其设置为“”。到目前为止,一切都很好。但现在我需要登录并检查 activation_key == ''.

这是我认为应该的,但没有到这里。

add_filter( 'authenticate', 'check_if_activated', 10, 3 );
function check_if_activated($user, $username, $password)
{
    // check if user has activated their email

    return $user;
}

Wordpress 已将过滤器添加到 authenticate,因此您必须注册较低的过滤器。

add_filter( 'authenticate', 'check_if_activated', 50);
function check_if_activated($user)
{
    // If we have an error, no need to check the activation key
    // (Wrong credentials, for instance)
    if (is_wp_error($user))
    {
        return $user;
    }
    // Checks the meta and returns an error if needed
    $validation_key = get_user_meta($user->ID, 'activation_key', true);
    return empty($validation_key) ? $user : new WP_Error('your_plugin_error_code', 'your error message');
}