最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to customize user rest api?

programmeradmin5浏览0评论

I am building a website VueJS and wordpress rest api. I am now working on author rest api. When i send get users :

get 

I have the following result :

[
    {
        "id": 1,
        "name": "Christian DOE",
        "url": "",
        "description": "Whats'up men ",
        "link": "/",
        "slug": "christian,
        "avatar_urls": {
            "24": ";d=mm&r=g",
            "48": ";d=mm&r=g",
            "96": ";d=mm&r=g"
        },
        "meta": [],
        "acf": [],
        "_links": {
            "self": [
                {
                    "href": ""
                }
            ],
            "collection": [
                {
                    "href": ""
                }
            ]
        }
    },
    {
        "id": 3,
        "name": "Jean Charles",
        "url": "",
        "description": "It's me ",
        "link": "/",
        "slug": "charles",
        "avatar_urls": {
            "24": ";d=mm&r=g",
            "48": ";d=mm&r=g",
            "96": ";d=mm&r=g"
        },
        "meta": [],
        "acf": [],
        "_links": {
            "self": [
                {
                    "href": ""
                }
            ],
            "collection": [
                {
                    "href": ""
                }
            ]
        }
    },
    {
        "id": 2,
        "name": "utilisateurtest",
        "url": "",
        "description": "Me aand me ",
        "link": "/",
        "slug": "utilisateurtest",
        "avatar_urls": {
            "24": ";d=mm&r=g",
            "48": ";d=mm&r=g",
            "96": ";d=mm&r=g"
        },
        "meta": [],
        "acf": [],
        "_links": {
            "self": [
                {
                    "href": ""
                }
            ],
            "collection": [
                {
                    "href": ""
                }
            ]
        }
    }
]

My aim is to customize users rest api : i would like to have a list of user social link users.

Is there a plugin i can use to : 1. to set the user social link ? 2. To get those links exposed by rest api ?

I've tried the following solution :

1.I upload a wordpress plugin to add some data. It's Simple Author plugin I am able to add some additional data to user ( social link list ) 2. I create a plugin to add this meta data be filled when i retrieved users from rest api. Here my plugin code :

 add_action( 'rest_api_init', 'adding_user_meta_rest' );

    function adding_user_meta_rest() {
       register_rest_field( 'user',
                            'collapsed_widgets',
                             array(
                               'get_callback'      => 'user_meta_callback',
                               'update_callback'   => null,
                               'schema'            => null,
                                )
                          );
    }


    function user_meta_callback( $user, $field_name, $request) {
       return get_user_meta( $user[ 'id' ], $field_name, true );
    }

I've tried a rest api get users request but i did not have the meta data listed in my response.

If you have an idea, i would like to know.

Thanks for your help.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论