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;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan