Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 在wp_usermeta值中更改用户rol?_Wordpress - Fatal编程技术网

Wordpress 在wp_usermeta值中更改用户rol?

Wordpress 在wp_usermeta值中更改用户rol?,wordpress,Wordpress,我需要在用户访问我网站的任何页面时,根据数据库中的数据值更改用户rol 在wp_usermeta的my DB中,有以下行: meta_值可以取值1和0 然后,我需要添加一个钩子,在加载站点的任何页面时触发,以运行一个代码,当“meta_key”的“meta_value”为“1”时,该代码将用户角色更改为“myCustomRole”。比如: add_action('template_redirect', 'hooker'); function hooker(){ $id_logged_u

我需要在用户访问我网站的任何页面时,根据数据库中的数据值更改用户rol

在wp_usermeta的my DB中,有以下行:

meta_值可以取值1和0

然后,我需要添加一个钩子,在加载站点的任何页面时触发,以运行一个代码,当“meta_key”的“meta_value”为“1”时,该代码将用户角色更改为“myCustomRole”。比如:

add_action('template_redirect', 'hooker');
function hooker(){
    $id_logged_user = $current_user_id = get_current_user_id();
    $table_name = "wp_usermeta";
    $results = $wpdb->get_results( "SELECT * FROM $table_name WHERE 'user_id' == $id_logged_user && 'meta_value' == 1 && 'meta_key'== 'wpuef_cid_c17'); 
      if($results) {
      $wp_user_object = new WP_User($current_user->ID);
      $wp_user_object->set_role('my_custom_role');
    }
}

inithook更适合更新用户角色

add_操作('init','changeUserRole');
函数changeUserRole()
{
$user\u id=get\u current\u user\u id();
$user_meta=get_user_meta($user_id,'wpuef_cid_c17',true);
如果($user\u meta==1){
$user=新的WP\u用户($user\u id);
$user->set_role('my_custom_role');
}
}

init hook更适合更新用户角色

add_操作('init','changeUserRole');
函数changeUserRole()
{
$user\u id=get\u current\u user\u id();
$user_meta=get_user_meta($user_id,'wpuef_cid_c17',true);
如果($user\u meta==1){
$user=新的WP\u用户($user\u id);
$user->set_role('my_custom_role');
}
}