1

I'm using this code to display in the user profile backend a field that will show the registration date of the user.

When I change the date and hit save, the registration date wont change, it will keep showing the original date.

How can I make this work?

Thanks

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

function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Información AbIbBEV", "blank"); ?></h3>

<table class="form-table">
<tr>
<th><label for="user_registered"><?php _e("Fecha de ingreso Empleado"); ?></label></th>
<td>
<input type="text" name="user_registered" id="user_registered" value="<?php echo esc_attr( get_the_author_meta( 'user_registered', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Fecha de ingreso del empleado."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta ( $user_id, 'user_registered', $user->user_registered );

}

1 Answer 1

0

The user_registered field is not in wp_usermeta, but wp_user. You should be using wp_update_user:

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

function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Información AbIbBEV", "blank"); ?></h3>

<table class="form-table">
<tr>
<th><label for="user_registered"><?php _e("Fecha de ingreso Empleado"); ?></label></th>
<td>
<input type="text" name="user_registered" id="user_registered" value="<?php echo esc_attr( get_the_author_meta( 'user_registered', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Fecha de ingreso del empleado."); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) {
        return false;
    }

    wp_update_user(
        [
            'ID'              => $user_id,
            'user_registered' => $user->user_registered,
        ]
    );
}

You may need to adjust your other code to pull from the user's data and not their meta.

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.