wordpress rest api response sending html content type issue with forward slash in URL

[亡魂溺海] 提交于 2021-02-19 06:17:21

问题


From my custom Wordpress rest api, i need to return below text as content type html.

OK ImageSendURL=www.yourdomain.xxx/Plugin/DownloadOrders

Here is my code to return the same

return new WP_REST_Response( "OK  \n  URL={$options['url']}", 200, array('content-type' => 'text/html; charset=utf-8'));

But this returns

"OK  \n  URL=http:\/\/yourdomain.xxx\/Plugin\/DownloadOrders"

I don't want the leading and trailing double quotes " and also the URL is kind screwed up. how can i fix this?

with the below code:

echo "OK  \n  ImageSendURL={$options['url']}";
return new WP_REST_Response( "", 200, array('content-type' => 'text/html; charset=utf-8'));

i got

OK ImageSendURL=http://yourdomain.xxx/Plugin/DownloadOrders""

now not sure how to get rid of the trailing ""


回答1:


the quotes are there because it's using the WP_HTTP_Response class that trying to give you a json as a response (uses the json_encode() function)

Don't do 'return', just do

header('Content-Type: text/html')
echo '<your html here>';
exit();

the exit function don't let the Wordpress to use WP_HTTP_Response class



来源:https://stackoverflow.com/questions/49365576/wordpress-rest-api-response-sending-html-content-type-issue-with-forward-slash-i

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!