'WP Json Endpoint with file extension .json
I want my wp json endpoint to end with .json file extension. I have tried doing regular expression but it didn't work. What did i do wrong? Or is it even possible?
Intention to have url as: domain.com/wp-json/feed_mmagic/v1/article/123.json
add_action('rest_api_init', function () {
register_rest_route( 'feed_magic/v1', 'article/(?P<feed_id>\d+)(.json)',array(
'methods' => 'GET',
'callback' => 'json_get_article'
));
});
function json_get_article($request){
header('Content-Type: application/json; charset=utf-8');
global $wp_query;
$feedid = $request['feed_id'];
$content = get_post( $feedid );
$response = new WP_REST_Response($content);
return $response;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|