0

i have add new fields when we create new user in user-new.php

the new field appears in the form, but the form data not saved into DB.

after i created new user, i go to "edit user" and see the field is empty.

any sugassins to make this work?

here is the code:

function custom_user_profile_fields($user){
    ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="company">Company Name</label></th>
            <td>
                <input type="text" class="regular-text" name="company" value="<?php echo esc_attr( get_the_author_meta( 'company', $user->ID ) ); ?>" id="company" /><br />
                <span class="description">Where are you?</span>
            </td>
        </tr>
    </table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );

function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    if(!current_user_can('manage_options'))
        return false;

    # save my custom field
    update_usermeta($user_id, 'company', $_POST['company']);
}
add_action('user_register', 'save_custom_user_profile_fields');

Update Thanks to @cybmeta, the code working great in single website but not in the multisite.

i think the problem is that the user not created right away. the user created after the user click on "activation link" in the email his get from wordpress.

  • Please, keep one question at the same time. – cybmeta Dec 11 '14 at 8:31
1

I've found these errors in your code:

  • The saving proccess of custom user fields is hooked to registration but not to update user profile actions.
  • You are using update_usermeta, a obsolete function, use update_user_meta instead.
  • You are using get_the_author_meta with $user->ID without checking before if $user is an object, which generate errors on new user registration form (also I recommend use get_user_meta instead).
  • You are not sanitze/validate the data of company field (I'm not sure what type of data you expect here, propposed saniteze_text_field as example).

I've made some changes and tested.

function custom_user_profile_fields($user){
    $previous_value = '';
    if( is_object($user) && isset($user->ID) ) {
        $previous_value = get_user_meta( $user->ID, 'company', true );
    }
    ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="company">Company Name</label></th>
            <td>
                <input type="text" class="regular-text" name="company" value="<?php echo esc_attr( $previous_value ); ?>" id="company" /><br />
                <span class="description">Where are you?</span>
            </td>
        </tr>
    </table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );

function save_custom_user_profile_fields($user_id){

    if(!current_user_can('manage_options'))
        return false;

    # save my custom field
    if( isset($_POST['company']) ) {
        update_user_meta( $user_id, 'company', sanitize_text_field( $_POST['company'] ) );
    } else {
        //Delete the company field if $_POST['company'] is not set
        delete_user_meta( $user_id, 'company', $meta_value );
    }
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action( 'personal_options_update', 'save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_custom_user_profile_fields' );

Also, this piece of code block any user to save the data if he/she has not "manage_options" capability, which is a capability usually only administrators have, so users won't can update these fields:

if(!current_user_can('manage_options'))
    return false;

So, remove it or make another checking of user capabality if it is needed. For example, checking if current user can edit the user being update seems better:

if ( !current_user_can( 'edit_user', $user_id ) )
    return false;
  • Hi @cybmeta i know what's that code do. the user that create the users is administrator. but i also tried to remove the "if" and still the db not updated. after i have created new user, i go to edit the user and see that this field is empty. any suggestions? – need-help Dec 10 '14 at 20:17
  • I'm sorry, I post a quick answer without take a closer look into your code. See my answer again. – cybmeta Dec 10 '14 at 21:48
  • Hi @cybmeta, Thanks for the help! but still when i create new user the company field is empty when i go to edit the user. but in edit mode, when i save some data in the company field, the data is saved. i have disable all plugins and using clean wordpress theme to be sure that i dont have any other conflict with this code. what do you think can be the problem here? – need-help Dec 11 '14 at 5:29
  • Hi @cybmeta, i have test your code in a single site and its working! but in multisite this code dont save the company field in the DB. i think the problem is that the user not created right away. the user created after the user click on "activation link" in the email his get from wordpress. i have update my question regarding this problem. – need-help Dec 11 '14 at 6:51
  • You never said that you are in multisite. Please, provide all relevant information if you want help, otherwise we are just wasting time. – cybmeta Dec 11 '14 at 8:30

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.