I have wrote an endpoint in WordPress and it works just fine. The problem is with the URL. The current format of the URL request must be like this:
/msisdn=019009
However, The URL request must be in this format:
;msisdn=019009
How do I format that?
Here is my code:
register_rest_route( 'zaindob/v1', '/sync_order/' . 'key=' . '(?P<key>\d+)' . '/msisdn=' . '(?P<msisdn>\d+)' , array(
'methods' => 'GET',
'callback' => 'updatetable',
) );
} );
I have wrote an endpoint in WordPress and it works just fine. The problem is with the URL. The current format of the URL request must be like this:
https://iotkidsiq/wp-json/zaindob/v1/sync_order/key=011900/msisdn=019009
However, The URL request must be in this format:
https://iotkidsiq/wp-json/zaindob/v1/sync_order?key=011900&msisdn=019009
How do I format that?
Here is my code:
register_rest_route( 'zaindob/v1', '/sync_order/' . 'key=' . '(?P<key>\d+)' . '/msisdn=' . '(?P<msisdn>\d+)' , array(
'methods' => 'GET',
'callback' => 'updatetable',
) );
} );
Share
Improve this question
edited Sep 5, 2019 at 15:04
user174452
asked Sep 5, 2019 at 14:43
Ahmed Dawood SalmanAhmed Dawood Salman
111 bronze badge
1 Answer
Reset to default 1You don't. Query args are not part of the route URL. Your endpoint URL is:
https://iotkidsiq/wp-json/zaindob/v1/sync_order
So needs to be registered as:
register_rest_route( 'zaindob/v1', '/sync_order', array(
'methods' => 'GET',
'callback' => 'updatetable',
) );
key
and msisdn
are arguments that are sent to your endpoint. To define these use set the args
property of the endpoint options:
register_rest_route( 'zaindob/v1', '/sync_order', array(
'methods' => 'GET',
'callback' => 'updatetable',
'args' => array(
'key' => array(
'type' => 'integer',
'required' => true,
),
'msisdn' => array(
'type' => 'integer',
'required' => true,
),
),
) );
Now your endpoint callback can accept the key
and msisdn
paramaters, and they are required for the endpoint to return a result:
function updatetable( $request ) {
$key = $request->get_param( 'key' );
$msisdn = $request->get_param( 'msisdn' );
// etc.
}