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

如何使用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;
    
    }
    
    点赞 评论

相关推荐 更多相似问题