0

I added a custom field to the profile-page of WordPress users called "uidnumber" and it reads values from the database just fine, however it just doesn't save anything.

wp_usermeta table

I'd rather save it in wp_users instead if I could because the whole table is just that much more clear, however from what I've read, WordPress updates could delete custom columns.

Here's what I got in my functions.php:

add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );

function save_extra_profile_fields( $user_id )
{
    update_user_meta( $user_id,'uidnumber', sanitize_text_field( $_POST['uidnumber'] ) );
}

and here's the field itself :

<p class="form-row form-row-wide">
                <label for="uidnumber"><?php _e( 'UID-Nummer', 'woocommerce' ); ?></label>
                <input type="text" class="input-text" name="uidnumber" id="uidnumber" value="<?php echo esc_attr( $user->uidnumber); ?>" />
            </p>

I have no idea why it won't save this field, while another one I coded into the "contact_methods", works like a charm.

edit01:

I tried what you told me, still doesn't save unless I put it in the wrong place.

<p class="form-row form-row-wide">
                <label for="uidnumber"><?php _e( 'UID-Nummer', 'woocommerce' ); ?></label>
                <input type="text" class="input-text" name="uidnumber" id="uidnumber" value="<?php echo get_user_meta( $user->ID, 'uidnumber', true ); ?>" />
            </p>

Or did you mean to put it in my save function? (As you mentioned value I assumed otherwise)

edit02: I think my main problem is that what you specified in your "show"-function is actually inside another file in my case: form-edit-account.php So I can't just use your "show_extra_profile_fields" function.

Also one thing that seems to be misunderstood: I want the user (normal user on WordPress) to be able to add his own text. That's the only reason I'm doing this. Personally I can add a UID-Number in the backend just fine. However that means I have to find out the UID for each and every user myself.

edit03:

functions.php - Here only the last couple lines are actually relevant.

form-edit-account.php

1
  • Well it's saving something, just it's blank! Is $user->uidnumber set (in the <input>'s value attribute)? Try get_user_meta( $user->ID, 'uidnumber', true ) instead...
    – bonger
    Commented Oct 7, 2015 at 17:02

1 Answer 1

1

You have not provided the whole code so don't understand where you are wrong, this script is working fine for me.

add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );

function save_extra_profile_fields( $user_id )
{
    if ( current_user_can('edit_user',$user_id) )
        update_user_meta($user_id, 'uidnumber', sanitize_text_field($_POST['uidnumber']));
}

add_action('show_user_profile', 'show_extra_profile_fields');
add_action('edit_user_profile', 'show_extra_profile_fields');

function show_extra_profile_fields( $user )
{
    ?>
        <p class="form-row form-row-wide">
            <label for="uidnumber"><?php _e( 'UID-Nummer', 'woocommerce' ); ?></label>
            <input type="text" class="input-text" name="uidnumber" id="uidnumber" value="<?php echo get_user_meta( $user->ID, 'uidnumber', true ); ?>" />
        </p>
    <?php
}

EDIT : For frontend code

You have to save uidnumber through update_user_meta here is the code

global $user;

$user = wp_get_current_user();

if ( $_POST )   {

    update_user_meta( $user->ID, 'uidnumber', sanitize_text_field($_POST['uidnumber']) );
}

EDIT 2 : personal_options_update action only works in admin section, you have to put that code into front end check pastebin

enter image description here

  1. sanitize_text_field
  2. show_user_profile
  3. edit_user_profile
  4. personal_options_update
  5. edit_user_profile_update
5
  • Does not work in my case. Like I said in my edit, it's the frontend, not the backend. Backend edits work just fine. If you need more information/more code, I'd be happy to provide you with such.
    – theHubi
    Commented Oct 8, 2015 at 9:19
  • @theHubi updated code hope this what you are looking for, otherwise need more info. Happy Coding!
    – Webloper
    Commented Oct 8, 2015 at 10:07
  • Maybe there are two lines of code conflicting each other or something..
    – theHubi
    Commented Oct 8, 2015 at 11:25
  • check edit 2 @theHubi
    – Webloper
    Commented Oct 8, 2015 at 11:38
  • I see. I added the code to my php edit-account file, still isn't working. But I'm pretty sure that this is a step in the right direction. Thanks a lot so far! I'll go check my functions.php again to see if I can figure something out.
    – theHubi
    Commented Oct 8, 2015 at 13:41

Your Answer

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

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