2

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!

2
  • What kind of text format? Can you give an example and show what you tried so far?
    – birgire
    Nov 14, 2016 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, 2016 at 19:52

1 Answer 1

7

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',
    ) );
} );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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