用户配置文件中的可编辑注册日期字段

时间:2019-02-08 作者:kiwii

我使用此代码在用户配置文件后端显示一个字段,该字段将显示用户的注册日期。

当我更改日期并单击“保存”时,注册日期不会更改,它将继续显示原始日期。

我怎样才能做到这一点?

谢谢

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 个回复
SO网友:phatskat

这个user_registered 字段不在中wp_usermeta, 但是wp_user. 您应该使用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,
        ]
    );
}
您可能需要调整其他代码,以从用户的数据而不是元数据中提取。