Unless I end Ajax processing function by exit() or die(), the Ajax function receives desired and correct output but following by 0.

Any idea why is that happening?

Is that meant to be like that or it could be fixed?

add_action('wp_ajax_get_homepage', 'get_homepage');
add_action('wp_ajax_nopriv_get_homepage', 'get_homepage');

function get_homepage(){
    echo "get_homepage ->";
    exit();
}

I’ve read at AJAX in Plugins, but why is that necessary?

1

The default response from admin-ajax.php is,

die( '0' );

…by adding your own wp_die() or exit() or die() after returning your desired content prevents the default response from admin-ajax.php being returned as well.

It also generally means that your ajax call has succeeded.

Ultimately, to answer your question, it’s meant to work this way. What you are doing by exiting after returning your content is the right thing to do.

Leave a Reply

Your email address will not be published. Required fields are marked *