通过 URL [WordPress] 更改用户角色 [英] Change user role via URL [WordPress]

查看:28
本文介绍了通过 URL [WordPress] 更改用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建插件,它将所有新用户注册为角色 = 未验证并阻止用户登录,直到电子邮件验证.我想在他们的电子邮件中发送 url,这会将当前未经验证的角色更改为作者.我知道如何在 WordPress 中发送电子邮件等,但不知道如何创建 url 来改变角色.我正在使用自定义 ajax 表单登录、注册和丢失密码,因为我无法使用 pie register 和 register-plus-redux 插件.

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. 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. 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.

当前代码

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:

生成链接

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


链接输出

[http:yourwebsite.com/a=fe440709d341e7b4994636b12e556aa7f23bb9ce&b=fe440709d341e7b4994636b12e556aa7f23bb9ce&7f23bb9ce&407a3b9ce&4b73b9ce&4b37e4b9ce&4b37e4b9ce&4b7e4b9ce&4b7e4b37e4b374b374b3c=4b9e2

当用户点击您刚刚发送给他们的电子邮件中的此链接时,使用此方法来捕获进入站点的 GET 参数:


Output of link

[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:

从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');

这篇关于通过 URL [WordPress] 更改用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆