dougehe2022 2017-06-19 13:17
浏览 32

添加新字段以编辑用户页面

I am trying to add a new field to my user edit page but I get this error

Notice: Object of class WP_User could not be converted to int in C:\xampp\htdocs\wordpress\wp-includes\capabilities.php on line 49

Here is the code I use for the functions,

    static function my_extra_user_fields( $user_id ) {
        echo $user_id->ID;
    $user_meta = get_user_meta($user_id);
     if (!empty ($user_meta['_is_post_agent'][0])) {
     $check_true = $user_meta['_is_post_agent'][0]; 
         }
     else { 
         $check_true="false"; 
         }
     ?>
        <h3>Agent Author</h3>

        <table class="form-table">
            <tr>
                <th><label for="agent_author">Agent Author</label></th>
                <td>

                    <input type="checkbox" name="agent_author" value="is_author_agent"  <?php if($check_true == 'true') echo 'checked="checked"';?> >

                </td>
            </tr>
        </table>
    <?php }



    static function save_my_extra_user_fields( $user_id ) {

        if ( !current_user_can( 'edit_user', $user_id ) ) { 

               return false;

     }else{

            if(isset($_POST['agent_author']) && $_POST['agent_author'] == true) {

             update_user_meta( $user_id, '_is_post_agent', 'true');
                }
                elseif(isset($_POST['agent_author']) && $_POST['agent_author'] == false) {

                    update_user_meta( $user_id, '_is_post_agent', 'false');
                }
                     }
                         }

I would appreciate any help as to what I am doing wrong here!

  • 写回答

2条回答 默认 最新

  • drll42469 2017-06-19 13:49
    关注

    get_user_meta() expects the first parameter to be an integer (or something that can be converted to an integer. If that parameter can't be converted to an integer, you will get that error.

    Try switching line 3 above to:

    $user_meta = get_user_meta($user_id->ID);
    

    As per the Wordpress documentation, it is expecting that to be an integer.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序