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?

  • what caching of any kind have to do with offline reading? if you need to store a response in your app, just store it. – Mark Kaplun Aug 18 '17 at 12:24
  • I'm using Volley which will automatically cache the response. But I it is not caching only WP rest API response. Do I need to add any headers to say volley to cache them? – Amar Ilindra Aug 18 '17 at 12:27
  • no idea what is volley ;) just doesn't sound right that caching info in an app depends on headers sent by the server. You can write a plugin to set headers on specific request, just sounds weird. – Mark Kaplun Aug 18 '17 at 12:31
  • Caching in the application depends on the headers of response we received. Read this: devcenter.heroku.com/articles/… I'm not a server guy and I don't know the exact terms to describe it. – Amar Ilindra Aug 18 '17 at 12:33

You should create a new instance from WP_REST_Response to set the Cache-Control value.

register_rest_route('wp/v2', '/your_endpoint', array(
    'methods' => 'GET',
    'callback' => 'function_callback',

function function_callback($data) {
  $response = array(

  $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.

  • If the response is different depending on the $data that gets requested, will the $response that gets cached be specific to that $data, or would it just return the same cached response from previous requests? – Best Dev Tutorials Nov 21 '20 at 15:07
  • Will return the new response. – Mostafa Soufi Nov 22 '20 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.