dpmir1988 2018-06-08 09:05
浏览 92
已采纳

如何使用POST请求保存值数组? (Wordpress API)

I'm trying to work with the Wordpress API to update a user's information via a POST request. The information is stored in a custom JSON field, that I registered like so:

public function handle_user_info() {
    register_rest_field( 'user', 'personal_info', array(
        'get_callback' => array( $this, 'get_user_info_callback' ),
        'update_callback' => array( $this, 'post_user_info_callback' ),
        'schema' => null 
    ));        
} 

Here's the GET callback:

public function get_user_info_callback( $user ) {  
    $userID = $user[ 'id' ]; 
    return array(
        'rcp_user_first' => get_user_meta( $userID, 'rcp_user_first', true ),
        'rcp_user_last' => get_user_meta( $userID, 'rcp_user_last', true ),
        'rcp_user_location' => get_user_meta( $userID, 'rcp_user_location', true ),
        'rcp_postal_address' => get_user_meta( $userID, 'rcp_postal_address', true ),
        'rcp_email' => get_userdata( $userID )->user_login
    );           
}

All of the above works fine. When I make a GET request, here's what is returned (shortened version):

{
    "id": 1,
    "name": "John",
    "url": "http://example.com",
    "description": "",
    "slug": "admin",

    ...

    "personal_info": {
        "rcp_user_first": "John",
        "rcp_user_last": "Doe",
        "rcp_user_location": "EU",
        "rcp_postal_address": "101 Fake Street",
        "rcp_email": "admin@example.com"
    }

    ...

}

My problem is with the POST callback. I can't figure out how to write it to update the values in the personal_info field. My guess was that something like this would work:

public function post_user_info_callback( $value, $user, $fieldName ) { 

    return array(
        'rcp_user_first' => update_user_meta( 1, 'rcp_user_first', 'Mark' ),
        'rcp_user_last' => update_user_meta( 1, 'rcp_user_last', 'Smith' ),
        'rcp_user_location' => update_user_meta( 1, 'rcp_user_location', 'NON_EU' ),
        'rcp_postal_address' => update_user_meta( 1, 'rcp_postal_address', '202 Fantasy Street' )
    );           

}

However this does not work. By the way I'm only testing with Postman right now which is why I'm using static data, so 1 here stands for the user ID.

Any ideas?

  • 写回答

1条回答 默认 最新

  • dsbx40787736 2018-06-08 11:30
    关注

    First you need to make sure you POST with the personal_info value so the callback will run.

    For example add in the postman personal_info[rcp_user_first] with some value. Now the callback will run and your static data will update.

    And then you can write some function that update dynamic every user key and value pair.

    public function post_user_info_callback( $value, $user, $fieldName ) {
    
        $defaultKeys = [
            'rcp_user_first' => '',
            'rcp_user_last' => '',
            'rcp_user_location' => '',
            'rcp_postal_address' => ''
        ];
    
        // filter the value so that we get only our specific keys that we want to update
        $valueArray = array_filter( array_merge( $defaultKeys, array_intersect_key( $value, $defaultKeys ) ) );
    
        $updateArray = [];
        foreach($valueArray as $key=>$val) {
    
            // Here you can add another validation for values.
            $updateArray[$key] = update_user_meta($user->ID, $key, $val);
        }
    
        return $updateArray;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 我不明白为什么c#微软的官方api浏览器为什么不支持函数说明的检索,有支持检索函数说明的工具吗?
    • ¥15 ORBSLAM2框架跑ICL-NUIM数据集
    • ¥15 在我想检测ros是否成功安装时输入roscore出现以下
    • ¥30 老板让我做一个公司的投屏,实时显示日期,时间,安全生产的持续天数,完全没头绪啊
    • ¥15 Google Chrome 所有页面崩溃,三种解决方案都没有解决,我崩溃了
    • ¥20 使用uni-app发起网络请求,获取重定向302返回的cookie
    • ¥20 手机外部浏览器拉起微信小程序支付 (相关搜索:微信小程序)
    • ¥20 怎样通过一个网址找到其他同样模版的网址
    • ¥30 XIAO esp32c3 读取FDC2214的数据
    • ¥15 在工控机(Ubuntu系统)上外接USB蓝牙硬件进行蓝牙通信