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!

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

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)