WordPress admin-ajax.php returns php code instead of json

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.

Read More

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;
}

Related posts

2 comments

  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
     */
    
  2. 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.

Comments are closed.