我正在尝试从数据库中的wp_users
表中获取加密的user_pass
数据,以显示为自定义的可编辑和可更新字段(如电子邮件字段(在管理端的"编辑用户"页面中。我想出了以下代码:
if ( is_admin() ) {
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>Additional Account Information</h3>
<table class="form-table">
<tbody>
<tr>
<th style="width:20.5%!important;">Existing Password (Encrypted)</th>
<td>
<input type="text" name="user_pass" id="user_pass" value="<?php echo esc_attr($user->user_pass) ?>" class="regular-text" />
</td>
</tr>
</tbody>
</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; }
$user_id = wp_update_user( array( 'ID' => $user_id, 'user_pass' => $_POST[ 'user_pass' ] ) );
}
}
更新:
上面的代码正在工作,但是当它更新时,它会重新加密我正在复制和粘贴的已经加密的密码。从而使加密密码成为密码本身!我只是希望它以我粘贴的方式保存。这可能吗?
我想要这些数据,以便能够将其从我的站点中的一个用户区域复制并粘贴到另一个用户区域,因为他们不共享数据库,但是有些用户需要在多个数据库中使用相同的信息,包括密码。我希望能够直接从Wordpress Admin区域而不是phpMyAdmin中执行此操作。
尝试替换它:
<input type="text" name="user_pass" id="user_pass" value="<?php echo esc_attr( get_userdata( $user->user_pass, $user->ID ) );; ?>" class="regular-text" />
通过这个:
<input type="text" name="user_pass" id="user_pass" value="<?= esc_attr($user->user_pass) ?>" class="regular-text" />
编辑:
以下是更新数据库中密码字段的代码:
add_action( 'edit_user_profile_update', 'update_extra_user_fields' );
add_action( 'personal_options_update', 'update_extra_user_fields' );
function update_extra_user_fields($user_id) {
global $wpdb;
if (!current_user_can('edit_user', $user_id)) return;
$user = get_user_by('ID', $user_id);
if (isset($_POST['user_pass']) && !empty($_POST['user_pass'])) {
if ($user->user_pass != $_POST['user_pass']) {
$wpdb->update(
$wpdb->users,
array(
'user_pass' => $_POST['user_pass'],
),
array( 'ID' => $user_id )
);
wp_cache_delete( $user_id, 'users' );
}
}
}
抱歉耽搁了!