0

I have a server.php file that I wish to host on the same server as my Wordpress site, that is required to send a POST message with a header of Content-type: application/json.

Wordpress is preventing me to use <?php header("ContentType : application/json"); ?> and doing so, it redirects to some file on https://site/wp-json/, which is not what I want.

Tried using the below code in the same file, but then it returns HTTP 500 Internal server error:

function changeHeaders($headers) {

    $headers['Content-Type'] = 'application/json';
    return $headers; 
}

add_filter('wp_headers', 'changeHeaders');

How do I change the headers just for that file without the need to create a subdomain or host it on another server without Wordpress?

  • 2
    Have you tried registering a REST API endpoint? Most of your server.php code could be copy pasted into it as is. Having standalone PHP files that bootstrap WP is a big security risk, there are better ways to do this that are faster/safer/easier – Tom J Nowell May 18 at 14:18
  • No I haven't but I will try. Thank you for pointing me in the right direction. – Srj May 18 at 15:31
  • I have looked into it but I don't think this will work with my setup since I have some external included files and some json files, as well which I constantly need to change. Will get a new SSL and make a subdomain to host my files. – Srj May 23 at 9:46

Your Answer

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

Browse other questions tagged or ask your own question.