【问题标题】:How can I add an updatable input field that shows the 'user_pass' encrypted data in the Edit Profile section?如何在“编辑配置文件”部分添加可更新的输入字段以显示“user_pass”加密数据?
【发布时间】:2019-12-06 06:16:09
【问题描述】:

我正在尝试从我的数据库中的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 管理区域而不是在 phpMyAdmin 中执行此操作。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    尝试替换这个:

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

    抱歉耽搁了!

    【讨论】:

    • 这确实显示了加密的密码,谢谢!但是我更改后它不会更新。它可以回到原来的那个......
    • 我编辑了我的答案以包含更新部分。告诉我这是否适合你。
    • 我在上面的代码中添加了更新。我试图让你的工作,但我得到一个 php 错误,它使我的网站崩溃,直到我删除你发布的函数。仅供参考,我将所有这些都发布在我的 functions.php 文件中。现在的问题是它正在重新散列我试图将其另存为的已经散列的密码。
    • 您是否删除了之前的“save_extra_user_profile_fields”功能?
    • 是的,我做到了。然后复制并粘贴您上面的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 2013-07-22
    • 2015-12-02
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多