WordPress REST API Custom Endpoint with URL Parameter

前端 未结 2 986
野性不改
野性不改 2020-12-31 10:04

I am trying to create a custom endpoint for the WordPress REST API and pass parameters through the URL.

The endpoint currently is:

/wp-json/v1/products

2条回答
  •  臣服心动
    2020-12-31 10:39

    I modified the provided answer a little to get my desired endpoint:

    /wp-json/api/v1/product?identifier=81838240219

    add_action( 'rest_api_init', function () {
    register_rest_route( 'api/v1', '/product/', array(
          'methods' => 'GET',
          'callback' => 'ea_get_product_data',
        ) );
    } );
    
    function ea_get_product_data( $data ) {
        $identifier = $data->get_param( 'identifier' );
        return $identifier;
    }
    

提交回复
热议问题