$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Editable registration date field in user profile|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Editable registration date field in user profile

matteradmin11PV0评论

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 );

}

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 );

}
Share Improve this question edited Feb 8, 2019 at 22:21 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Feb 8, 2019 at 22:14 kiwiikiwii 211 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far