I'm using WordPress rest API for developing my Android application. But I can not cache the response.
How can I add required headers to REST API response so that my app can save the response for offline reading?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI'm using WordPress rest API for developing my Android application. But I can not cache the response.
How can I add required headers to REST API response so that my app can save the response for offline reading?
You should create a new instance from WP_REST_Response
to set the Cache-Control
value.
<?php
register_rest_route('wp/v2', '/your_endpoint', array(
'methods' => 'GET',
'callback' => 'function_callback',
));
function function_callback($data) {
$response = array(
1,2,3,4,5,6,7,8
);
$result = new WP_REST_Response($response, 200);
// Set headers.
$result->set_headers(array('Cache-Control' => 'max-age=3600'));
return $result;
}
Click here to get more info about directives.