1

Is it possible to return plain text in a WordPress Rest API response? By default this seems to return JSON. Any advice on the recommended way to override this? Doing a json_decode() on my response object before it is returned from the callback function does not have any effect, so I assume the JSON gets created after this.

Thanks!

  • What kind of text format? Can you give an example and show what you tried so far? – birgire Nov 14 '16 at 18:52
  • I am trying to subscribe to a webhook and am being sent a challenge key in the query string, When I return the response string it has quotation marks around it (because it has been converted to json). I need to return the string value only, without any quotation marks. – anniec Nov 14 '16 at 19:52
3

You might be able to output your headers with the response. Then just kill the request with exit();.

The default is usually to return a value and let the process encode and output for you.


Based on http://v2.wp-api.org/extending/adding/

function my_awesome_func( $data ) {
    header("Content-Type: text/plain");
    print_r( array('foo'=>'bar'));
    exit();
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/test/', array(
        'methods' => 'GET',
        'callback' => 'my_awesome_func',
    ) );
} );

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.