简体   繁体   English

通过URL更改用户角色[WordPress]

[英]Change user role via URL [WordPress]

I'm creating plugin which will register all new users as role = unverified and stop user from login until email verification. 我正在创建一个插件,它将所有新用户注册为角色=未验证,并阻止用户登录直到进行电子邮件验证。 I want to send url on their email which will change the current unverified role to author. 我想在他们的电子邮件中发送url,这会将当前未验证的角色更改为作者。 I know how to send email etc all of that in WordPress but can't figure out how to create url which will change the role. 我知道如何在WordPress中发送电子邮件等所有内容,但无法弄清楚如何创建将更改角色的url。 I'm using custom ajax form to login,register and lostpassword because of it i'm unable to use pie register and register-plus-redux plugins. 我正在使用自定义ajax表单登录,注册和丢失密码,因为它无法使用饼状寄存器和register-plus-redux插件。

Current code 当前代码

function add_roles_on_plugin_activation() {
    add_role( 'custom_role', 'Unverified', array( 'read' => true, 'level_0' => true ) );
}
register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' ); 

function remove_roles_on_plugin_deactivation() {
    remove_role( 'custom_role' );
}
register_deactivation_hook( __FILE__, 'remove_roles_on_plugin_deactivation' ); 

add_filter('pre_option_default_role', function($default_role){
    return 'custom_role'; 
    return $default_role; 
});


function error_email_verify() {
$user = wp_get_current_user();
if ( in_array( 'custom_role', (array) $user->roles ) ) {

       $logout_url = wp_login_url().'?mode=emailverify';
       wp_logout();
       wp_redirect( $logout_url );
       exit();
    }     
}
add_action('wp_loaded', 'error_email_verify');

function my_login_message() {

    if( $_GET['mode'] == 'emailverify' ){
        $message = '<p id="login_error"><b>Verify your email.</b></p>';
        return $message;
    }

}
add_filter('login_message', 'my_login_message');

Here is what I would do. 这就是我要做的。 Basically I would generate a link that would have parameters attached to it something like this: 基本上,我会生成一个链接,该链接将带有如下所示的参数:

Generate Link 产生连结

$username = 'user_login';
$hashcode = sha1(md5(md5("hacaak".$username."aalog")));
$link = get_home_url().'/?a='.$hashcode.'&b='.$hashcode.'&u='.$username.'&c='.$hashcode.'';


Output of link 链接输出

[http:yourwebsite.com/a=fe440709d341e7b4994636b12e556aa7f23bb9ce&b=fe440709d341e7b4994636b12e556aa7f23bb9ce&u=jack&c=fe440709d341e7b4994636b12e556aa7f23bb9ce][1] [HTTP:yourwebsite.com/a=fe440709d341e7b4994636b12e556aa7f23bb9ce&b=fe440709d341e7b4994636b12e556aa7f23bb9ce&u=jack&c=fe440709d341e7b4994636b12e556aa7f23bb9ce] [1]

When the user clicks on this link in the email you just sent them use this method to catch the GET parameter coming into the site: 当用户单击您刚刚发送给他们的电子邮件中的此链接时,使用此方法可以捕获进入网站的GET参数:

function to get the variables from url 从URL获取变量的函数

function catch_email(){
    if(isset($_GET['a']))
    {
        $username = sanitize_user($_GET['u']);
        $user=get_user_by( 'login', $username );
        $user_id = $user->ID;
        $user_role = new WP_User($user_id);
        $user_role->remove_role( 'unverified' );
        $user_role->add_role( 'author' );
        $url = get_home_url().'/Login?mode=emailverify';
        wp_redirect($url);
    }
}

add_action('get_header', 'catch_email');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM