Support » Developing with WordPress » return plain text from register_rest_route

  • hi
    how to return plain text from register_rest_route? if i do return “peter”, the returning json will still have the double quote. I want to remove that.
    Thanks
    From Peter

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello there!

    You can remove double quotes from your string with this code.

    $string = str_replace('"', '', $string);

    Let me know if this solves your problem.

    It does not solve my problem. If i:

    return “peter”;

    it will give me:

    “peter”

    rather than:

    peter

    because it thinks it is a json string so auto add double quote to it

    Moderator bcworkz

    (@bcworkz)

    The REST API will return JSON data even though you return a simple string from your route endpoint callback. In JSON all strings are delimited by quotes. Use json_decode() to get back to a normal string value without quotes.
    echo json_decode( $response['body'] );
    If you did return 'Peter'; from your callback, the output of above code will be Peter.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘return plain text from register_rest_route’ is closed to new replies.