2

How do I make WordPress return JSON on Nginx?

In my wordpress, Ajax calls return <?php JSON instead just plain JSON. The site works just fine, except for admin-ajax calls.

Regular call SessionSniff

File upload call FileUploadSniff

TechInfo:

Wordpress 4.5
PHP 5.6.11-1ubuntu3.1 (fpm-fcgi)
nginx version: nginx/1.9.3 (Ubuntu)

Headers sniff:

Request URL:http://SERVER/wp-admin/admin-ajax.php
Request Method:POST
Status Code:200 OK
Remote Address:IP:80
Response Headers
view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://SERVER
Cache-Control:no-cache, must-revalidate, max-age=0
Connection:keep-alive
Content-Type:application/json; charset=UTF-8
Date:Tue, 26 Apr 2016 06:44:17 GMT
Expires:Wed, 11 Jan 1984 05:00:00 GMT
Pragma:no-cache
Server:nginx/1.9.3 (Ubuntu)
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Robots-Tag:noindex
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,pl;q=0.6
Connection:keep-alive
Content-Length:107
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:REDACTED ;)
Host:SERVER
Origin:http://SERVER
Referer:http://SERWER/wp-admin/media-new.php
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Data
view source
view URL encoded
data[wp-auth-check]:true
interval:60
_nonce:32b36eaa97
action:heartbeat
screen_id:media
has_focus:false

NGINX

location / {
    index index.php index.html index.htm;
            try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
}
2
  • 1
    It seems like you have extre <?php tag there somewhere in your code check it properly. Commented Apr 26, 2016 at 7:10
  • 1
    the WP installation is brand new. I will try @Fiil 's answer... Commented Apr 26, 2016 at 7:46

2 Answers 2

1

Just to close this topic. The problem was NO COMMENTS in functions.php file After I added the following to functions.php, the problem disappeared.

<?php
/**
 * Twenty Fifteen functions and definitions
 *
 * Set up the theme and provides some helper functions, which are used in the
 * theme as custom template tags. Others are attached to action and filter
 * hooks in WordPress to change core functionality.
 *
 * When using a child theme you can override certain functions (those wrapped
 * in a function_exists() call) by defining them first in your child theme's
 * functions.php file. The child theme's functions.php file is included before
 * the parent theme's file, so the child theme functions would be used.
 *
 * @link https://codex.wordpress.org/Theme_Development
 * @link https://codex.wordpress.org/Child_Themes
 *
 * Functions that are not pluggable (not wrapped in function_exists()) are
 * instead attached to a filter or action hook.
 *
 * For more information on hooks, actions, and filters,
 * {@link https://codex.wordpress.org/Plugin_API}
 *
 * @package WordPress
 * @subpackage Twenty_Fifteen
 * @since Twenty Fifteen 1.0
 */
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah it's correct, but in general, this problem arises because you don't have an empty line after <?php tag
0

Check if you wordpress files were not compromised. You probably have some modification made in core Wordpress files that adds "

Do fresh install of Wordpress files and check it it still exists. All modification to wordpress should be done in theme folder or plugins.

I problem still occurs try to disable all plugins and turn on one by one. It is possible that one of plugins is messing up.

5 Comments

completel fresh install, I removed ALL plugins, still the same :(
It looks like you have something wrong with nginx configuration. How does raw response for non AJAX requests looks like?
Non ajax works perfectly. WP works, I can edit posts and display them.. it's just AJAX that returns <?phpJSON
Did you tried fresh install of Wordpress (new folder, new database, default theme, no plugins)? Does non-AJAX responses begins with <?php as well (view source) - it might be cleaned by browser but exist.
yes I did. I completely removed all files and uploaded new ones. Removed everything from plugins, attached a new table in DB (can't create new DB). still nothing. Checked file permissions, nginx "fixpath" settings, php versions, headers, file owners... I'm lost :(

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.