admin管理员组文章数量:1328017
Having an admin account, I can change everything except the username (which happens to be the login name). I am therefore wondering how to change the username using the functions file.
Running the below as a SQL query works fine, but how do I "convert" it into a function?
add_action('init','username_query');
function username_query() {
global $wpdb;
$result = (UPDATE `wp_users` SET `user_login`= 'new-admin-name-here' WHERE `user_login`='old-admin-name-here');
}
Any help is appreciated and yes; I know that the user will be logged out and asked to login again.
Having an admin account, I can change everything except the username (which happens to be the login name). I am therefore wondering how to change the username using the functions file.
Running the below as a SQL query works fine, but how do I "convert" it into a function?
add_action('init','username_query');
function username_query() {
global $wpdb;
$result = (UPDATE `wp_users` SET `user_login`= 'new-admin-name-here' WHERE `user_login`='old-admin-name-here');
}
Any help is appreciated and yes; I know that the user will be logged out and asked to login again.
Share Improve this question asked Jul 21, 2020 at 4:59 Moa AndersenMoa Andersen 154 bronze badges1 Answer
Reset to default 1There are many ways you can achieve this... In your instance, as you pointed out $wpdb, here is the possible ways you can achieve this:
1. Use $wpdb::query
You can run this query directly using $wpdb's query method as:
add_action("init", "username_query")
function username_query(){
global $wpdb;
$prefix = $wpdb->prefix;
$query = "UPDATE `{$prefix}wp_users` SET `user_login`= 'new-admin-name-here' WHERE `user_login`='old-admin-name-here'";
$results = $wpdb->query( $query );
if( $results === false ){
return "Error updating admin";
}
return "Admin username updated";
}
2. Use $wpdb::update
You can also use $wpdb's update method to run your query as in below example
add_action("init", "username_query")
function username_query(){
global $wpdb;
$updated_rows = $wpdb->update(
"wp_users",
array( "user_login" => "new_admin_name_here" ),
array( "user_login" => "old_admin_name_here" ),
array( "%s" ),
array( "%s")
);
if ( $updated_rows === false ){
return "Error updating admin";
}
return "Admin username updated";
}
本文标签: mysqlRun Username SQL Query from WordPress Child Theme Functions File
版权声明:本文标题:mysql - Run Username SQL Query from WordPress Child Theme Functions File 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742245425a2439169.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论