admin管理员组文章数量:1023868
Using the User ID of an existing user, how do I change the role to Editor?
Here's what I tried:
$wpdb->query("UPDATE wp_usermeta SET meta_value = 'a:1:{s:6:\"editor\";b:1;}' WHERE user_id = '$user_id' AND meta_key = 'wp_capabilities'");
Not sure what I am doing wrong?
Using the User ID of an existing user, how do I change the role to Editor?
Here's what I tried:
$wpdb->query("UPDATE wp_usermeta SET meta_value = 'a:1:{s:6:\"editor\";b:1;}' WHERE user_id = '$user_id' AND meta_key = 'wp_capabilities'");
Not sure what I am doing wrong?
Share Improve this question asked Apr 19, 2019 at 20:42 32454325342523245432534252 1174 bronze badges1 Answer
Reset to default 0Usermeta key that stores the user's role has a prefix in the name, the one set in the wp-config.php
file. So meta_key
will have the form {prefix}_capabilities
and your query should look like
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->usermeta} SET meta_value = 'a:1:{s:6:\"editor\";b:1;}' WHERE user_id = %d AND meta_key = '{$wpdb->prefix}capabilities'",
$user_id )
);
You do not need to write SQL query. You can achieve the same by fetching the WP_User
object with get_userdata
function and set new role with the method set_role (or remove_role
and add_role
to change only one of the roles).
$my_user_id = 12345; // user ID for changing role
$my_user = get_userdata($my_user_id);
if ( $my_user instanceof WP_User )
$my_user->set_role('editor');
Using the User ID of an existing user, how do I change the role to Editor?
Here's what I tried:
$wpdb->query("UPDATE wp_usermeta SET meta_value = 'a:1:{s:6:\"editor\";b:1;}' WHERE user_id = '$user_id' AND meta_key = 'wp_capabilities'");
Not sure what I am doing wrong?
Using the User ID of an existing user, how do I change the role to Editor?
Here's what I tried:
$wpdb->query("UPDATE wp_usermeta SET meta_value = 'a:1:{s:6:\"editor\";b:1;}' WHERE user_id = '$user_id' AND meta_key = 'wp_capabilities'");
Not sure what I am doing wrong?
Share Improve this question asked Apr 19, 2019 at 20:42 32454325342523245432534252 1174 bronze badges1 Answer
Reset to default 0Usermeta key that stores the user's role has a prefix in the name, the one set in the wp-config.php
file. So meta_key
will have the form {prefix}_capabilities
and your query should look like
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->usermeta} SET meta_value = 'a:1:{s:6:\"editor\";b:1;}' WHERE user_id = %d AND meta_key = '{$wpdb->prefix}capabilities'",
$user_id )
);
You do not need to write SQL query. You can achieve the same by fetching the WP_User
object with get_userdata
function and set new role with the method set_role (or remove_role
and add_role
to change only one of the roles).
$my_user_id = 12345; // user ID for changing role
$my_user = get_userdata($my_user_id);
if ( $my_user instanceof WP_User )
$my_user->set_role('editor');
本文标签: wp querySet quoteditorquot role to existing user
版权声明:本文标题:wp query - Set "editor" role to existing user 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745569652a2156658.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论