最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Why does WordPress add 0 (zero) to an Ajax response?

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Oct 6, 2013 at 2:12 Radek asked Oct 4, 2013 at 11:58 RadekRadek 5951 gold badge11 silver badges26 bronze badges 2
  • Does it (the addition of 0) happen even if you're logged out of wordpress? – murdaugh Commented Oct 4, 2013 at 12:06
  • Yes, even if I am not logged into WordPress. – Radek Commented Oct 4, 2013 at 12:14
Add a comment  | 

1 Answer 1

Reset to default 40

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.

发布评论

评论列表(0)

  1. 暂无评论