0

I have field to change user meta but update user meta does not change

add_action('user_register', 'save_extra_fields_country_institution_user_form');
add_action('profile_update', 'save_extra_fields_country_institution_user_form');
add_action('edit_user_profile', 'save_extra_fields_country_institution_user_form' );
    function save_extra_fields_country_institution_user_form($user_id){
        # again do this only if you can
        if(!current_user_can('manage_options') || !current_user_can( 'edit_user' ))
            return false;

        # save my custom field
        update_user_meta($user_id, 'verifier_assign_country', $_POST['country-verifier']);
        update_user_meta($user_id, 'verifier_assign_institution', $_POST['institution-verifier']);
        update_user_meta($user_id, 'verifier_price', $_POST['user_price']);

    }
  • Have you confirmed that this data is in the $_POST global? Add print_r($_POST); die(); as the first line of your function and see what it says. – Peter HvD Aug 30 '18 at 10:51
0

Welcome, either use edit_user_profile_update which is passed user_id

add_action('edit_user_profile_update', 'save_extra_fields_country_institution_user_form' );
    function save_extra_fields_country_institution_user_form($user_id){
        # again do this only if you can
        if(!current_user_can('manage_options') || !current_user_can( 'edit_user' ))
           return false;
        # save my custom field
        $result = update_user_meta($user_id, 'verifier_assign_country', 'test3');
    }

OR when I tested on 'edit_user_profile', the whole user object is being passed, not just the user id, so use:

function save_extra_fields_country_institution_user_form($user){
    # again do this only if you can
    if(!current_user_can('manage_options') || !current_user_can( 'edit_user' ))
       return false;
    # save my custom field
    $result = update_user_meta($user->ID, 'verifier_assign_country', 'test');
}

However https://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile is more aimed at adding fields to screen, so I think what you want is 'edit_user_profile_update'

  • A good way to debug things like this is check everything: ie whether the action is firing, what you are being passed, any conditional logic and what is being returned by any function calls. In this case adding something like error_log('Just in '.print_r($user_id, true)); revealed that user_id was actually user object. It can also help to look at the actual code. core.trac.wordpress.org/browser/tags/4.9.8/src//wp-admin/… showed me that if we wanted to do it only on update, then edit_user_profile_update was better. – anmari Aug 30 '18 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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