I’m trying to update user_login
in the wp_users
table, to force update a user’s username to their email address on submission of a front end edit profile form.
I know WordPress forbids this via the wp_update_user
function so I am trying to use SQL with wpdb functions.
Here is what I have, and it is not working 🙁
global $wpdb;
$tablename = $wpdb->prefix . "users";
$sql = $wpdb->prepare( "UPDATE ".$tablename." SET user_login=".$user_email." WHERE ID=".$user_id."", $tablename );
$wpdb->query($sql);
Can anyone help?
Fixed it, see here: https://gist.github.com/4045215
First:
The
$wpdb
object has the names of tables, with prefixes, pre-defined for you.Second:
$wpdb-prepare()
is essentially a WordPress aware printf, if you pass it more than one argument, you need to have some string/digit replacements%s
%d
Alternatively
$wpdb
does have an update method as well:http://codex.wordpress.org/Class_Reference/wpdb
Fixed it! There are two methods:
Updated Gist: https://gist.github.com/4045215