在Wordpress自定义端点上添加链接

在Wordpress自定义端点上添加链接,wordpress,endpoint,wordpress-rest-api,Wordpress,Endpoint,Wordpress Rest Api,我在Wordpress插件的自定义表中为特定数据创建了自定义端点。它使用getHelpers()函数从表中获取所有数据。之后,它将被一些用户数据合并。我想添加profile_图像作为响应的链接,这样我们就可以使用embed参数获得它 将链接添加到响应的最佳方式是什么?我知道$response->add_link()函数,但这会将其添加到响应中,而不是每个参与者 我试图将链接添加为数组,但这不会对_embed参数起作用 这是我的自定义端点代码: class VEMS_Rest_Contributo

我在Wordpress插件的自定义表中为特定数据创建了自定义端点。它使用
getHelpers()
函数从表中获取所有数据。之后,它将被一些用户数据合并。我想添加profile_图像作为响应的链接,这样我们就可以使用embed参数获得它

将链接添加到响应的最佳方式是什么?我知道
$response->add_link()
函数,但这会将其添加到响应中,而不是每个参与者

我试图将链接添加为数组,但这不会对_embed参数起作用

这是我的自定义端点代码:

class VEMS_Rest_Contributors extends WP_REST_Controller {

    protected $namespace = 'vems/v2';

    protected $rest_base = 'contributors';


    /**
     * Register the routes for coupons.
     */
    public function register_routes() {

         register_rest_route( $this->namespace, '/' . $this->rest_base, array(
            'methods'   => WP_REST_Server::READABLE,
            'callback'  => array( $this, 'get_items' ),
            'args'      => $this->get_collection_params(),
        ) );
    }

    public function get_items( WP_REST_Request $request ) {

        $project_id = $request->get_param( 'project_id' );

        $contributors = array();

        if( !empty($project_id) ) {

            $project = new VEMS_Project( $request['project_id'] );
            $helpers = $project->getHelpers();

            foreach($helpers as $helper) {

                $contributor = array();

                if( !empty($helper->contributor_id) ) {
                    $user = get_user_by( 'ID', $helper->contributor_id );
                    $user_meta = get_user_meta( $helper->contributor_id );

                    $contributor['ID'] = $helper->contributor_id;
                    $contributor['user_nicename'] = $user->data->display_name;
                    $contributor['user_profile_image'] = $user_meta['contributor_profile_image'][0];
                } else {
                    $contributor['user_nicename'] = $helper->name;
                    $contributor['user_profile_image'] = $helper->image_id;
                }

                $contributor['item_total'] = $helper->item_total;
                $contributor['checked'] = $helper->checked;
                $contributor['helper_date'] = $helper->helper_date;

                /*
                $contributor['_links']['profile_image'] = array(
                        'href' => rest_url( '/wp/v2/media/' . $contributor['user_profile_image'] ), 
                        'embeddable' => true
                    ); 
                */

                $contributors[] = $contributor;
            }

        }   

        $response = rest_ensure_response( $contributors );

        return $response;
    }

    public function get_collection_params() {

        $params['project_id'] = array(
            'description'       => __( 'Limit result set to contributors assigned a specific project.', 'vems' ),
            'type'              => 'integer',
            'sanitize_callback' => 'absint',
            'validate_callback' => 'rest_validate_request_arg',
        );

        return $params;
    }

}

要处理路由
vems/v2/contributors?\u embed
上的链接,元素
profile\u image
必须是一个链接数组,然后才能执行此操作

            $contributor['_links']['profile_image'] = [
                [
                    'href' => rest_url( '/wp/v2/media/' . $contributor['ID'] ), 
                    'embeddable' => true,
                ],
            ];

你说的是哪个嵌入参数?默认的Wordpress rest api嵌入参数将链接嵌入json输出。就像您可以处理posts一样:/wp json/wp/v2/posts?\u嵌入有一个函数$response->add\u link();但我不知道它如何处理多个响应。我已经试着按照您的描述添加了这个。这将把它添加到json中,但当我添加?_embed=true参数时,它不会添加到_embedded键中。当我转到wp json/vems/v2/contributors/?\u embed=true时的响应仍然是:
{ID:“1”,user\u nicename:“Test”,user\u profile\u image:“938”,链接:{profile\u image:{href:”http://dev.nl/vems/wp-json/wp/v2/media/938“,Embeddedable:true}}}
找到了解决方案:[''u links']['profile\u image']需要是一个多数组。因此,
$contributor[''u links']['profile\u image']=array('href'=>rest\u url('/wp/v2/media/'。$contributor['user\u profile\u image']),'embeddeble'=>true)不起作用,但
$contributor[''u links']['profile\u image'][]=array('href'=>rest\u url('/wp/v2/media/'。$contributor['user\u profile\u image']),'embeddeble'=>true)将工作!