1

i'm having a trouble with Wordpress, with the function update_user_meta()

I'm trying to update or add a new custom meta value for the user, who already have the custom meta working on the edit profile. But when i do the function update_user_meta() outside the edit-profile.php, just didn't work for me.

I have a page like a page of contact where the form will edit the custom metadata of the profile, in the front side of the wordpress like a normal page of wordpress, but when he submit the form, he just don't update, here is my code:

when the user submit:

function update_termini() {
    $user = wp_get_current_user();
    $userData = array();
    $userData['checkbox'] = intval( $_POST['custom_user_fields_checkbox'] );
    update_user_meta( $user, 'custom_user_fields', $userData );
}
add_action('edit_user_profile', 'update_termini');

2 Answers 2

0

wp_get_current_user() returns an object. update_user_meta( $user_id, $meta_key, $meta_value, $prev_value ) accepts user_id as first parameter. Use the following code to get user ID from wp_get_current_user()

update_user_meta( $user->ID, 'custom_user_fields', $userData );
4
  • 1
    still not working :(
    – Rafael RN
    Aug 19, 2016 at 19:00
  • after $user = wp_get_current_user() do print_r($user) and see what is the ID of the user, to get this working you should be logged in. then check the {prefix}_usermeta table in WordPress for that user ID and see if the options is updated or not. Aug 19, 2016 at 19:03
  • no, isn't updated :C i'm getting mad
    – Rafael RN
    Aug 19, 2016 at 19:11
  • This approach will only work for the logged in user but will cause a data nightmare when the current logged in user who has privileges to edit users does; he/she will rather be updating his/her own records instead of that of the actual user subjected to the edit. May 30, 2017 at 23:36
1

This is the right way to update user meta so you don't mess up your data; the snippet is commented so as to provide more details:

function update_termini( $user_id ) {
//    print_r( $user_id );
//    exit;
// $user_id holds the user ID of the actual user's profile page subjected to the edit.
// That is the right way to do it.


// The comment block right below this very block is an ineffective way as it will only work
// for a current user editing his/her own profile page and why it is not the best way out;

// Using wp_get_current_user() to derive at a user ID as in: $user = wp_get_current_user();
// then, $user->ID; will enable you get the user ID of the CURRENT USER and that is going
// to AFFECT YOUR DATA NEGATIVELY when an administrator for instance edits the profile of
// a user; what will happen then is that the data of the custom field will actually update
// the records of the administrator rather than that of the intended user whos profile in
// being edited by the administrator.

// You can then perform any checks if needed,
    if ( current_user_can( 'edit_user' ) ) {
//      print_r( $_POST );
//      exit;

// And proceed to update the user meta of the actual user's profile page subjected to the edit;
// it may or may not be the current user.
// This way, you are sure to update the right field with right information.

// For the purpose of this update, it is assumed the name of our newly added custom field
// is custom_user_fields_checkbox.
        update_user_meta( $user_id, 'custom_user_fields_checkbox', $_POST['custom_user_fields_checkbox'] );
    }
}
add_action( 'edit_user_profile', 'update_termini' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.