du9843 2019-07-28 20:42
浏览 161
已采纳

如何在“编辑配置文件”部分中添加可更新的输入字段以显示“user_pass”加密数据?

I'm trying to get the encrypted user_pass data from my wp_users table in my database to show up as a custom editable and updatable field (like the email field) in the "Edit User" pages within the admin side only. I have come up with the following code:

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' ] ) );
    }
}

UPDATE:

The code above is working but when it updates it is re-encrypting the already encrypted password I am copy and pasting. Thereby making the encrypted password the password itself! I just want it to save the exact way I paste it in. Is this even possible?

I want this data so I am able to copy and paste it from one user area to another within my site as they do not share databases, however some users need to be in more than one database with the same information including password. I would like to be able to do that right from the Wordpress Admin areas instead of in phpMyAdmin.

  • 写回答

1条回答 默认 最新

  • dshnx48866 2019-07-28 22:12
    关注

    Trying replacing this :

    <input type="text" name="user_pass" id="user_pass" value="<?php  echo esc_attr( get_userdata( $user->user_pass, $user->ID ) );; ?>" class="regular-text" />
    

    by this :

    <input type="text" name="user_pass" id="user_pass" value="<?= esc_attr($user->user_pass) ?>" class="regular-text" />
    

    EDIT :

    Here's the code to update the password field in DB :

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

    Sorry for the delay!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?