Wordpress - admin-ajax.php returns 0. How do I debug it and fix it?

Enable Debugging

WordPress has constants defined in wp-config.php where you can print errors to the screen and log them in a separate file located /wp-content/debug.log. It looks like this:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );
define( 'WP_DEBUG_LOG', true );

You can then print your own information to the debug log at specific point and find out exactly where ( or if the function is even getting hit at all ) the function is erroring out:

function write_here_featured_image_upload() {
    error_log( 'Made it into the Ajax function safe and sound!' );
    /** ... Rest of your code ... **/
}

Check Dev Tools Console

Almost all browsers in this modern day have Developer Tools and a Console where Javascrpit errors are output to. If you see an error in your Dev Tools console you'll need to fix that first and foremost.


As for a possible solution, you have this conditional which is preventing you from running ajax on the front-end of the website:

if ( is_admin() ) {
    add_action( 'wp_ajax_write_here_img_upload', 'write_here_featured_image_upload' );
    add_action( 'wp_ajax_nopriv_write_here_img_upload', 'write_here_featured_image_upload' );
}

The function is_admin() tells WordPress to only run those actions whenever in the Admin Panel / Dashboard so you would never see anything happen on the front-end of your website. Try removing the conditional and just adding the action as is:

add_action( 'wp_ajax_write_here_img_upload', 'write_here_featured_image_upload' );
add_action( 'wp_ajax_nopriv_write_here_img_upload', 'write_here_featured_image_upload' );