wordpress从admin编辑wp_users表字段

时间:2016-10-20 10:26:16

标签: php wordpress

我在wp_users表中添加了一个字段“money”,用于存储用户的金额。现在我想让管理员能够更改输入字段值。

我可以通过添加到functions.php

的函数获取值
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

    function my_show_extra_profile_fields( $user ) { ?>

      <h3>User money</h3>

      <table class="form-table">

        <tr>
          <th><label for="twitter">Amount</label></th>

          <td>
            <input type="text" name="money" id="money" value="<?php echo esc_attr( get_the_author_meta( 'money', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description">Enter user money</span>
          </td>
        </tr>

      </table>
    <?php }

但我该如何更新该字段?

2 个答案:

答案 0 :(得分:0)

add_action('edit_user_profile_update', 'update_extra_profile_fields');

 function update_extra_profile_fields($user_id) {
     if ( current_user_can('edit_user',$user_id) )
         update_user_meta($user_id, 'money', $_POST['money']);
 }

请您尝试上面的代码吗?

答案 1 :(得分:0)

最后我以这样的方式做了

add_action('edit_user_profile_update', 'update_extra_profile_fields');

 function update_extra_profile_fields($user_id) {
     if ( current_user_can('edit_user',$user_id) )

      $money = $_POST['money'];

      global $wpdb;


      $wpdb->query("UPDATE wp_users SET money='$money' WHERE ID = '$user_id'");
 }