Why is die() used at the end of function that handles an Ajax request?

Multi tool use
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
6
down vote
favorite
What could go wrong if I am not using die();
after the function that handles an Ajax request?
I have noticed that almost all the WordPress Ajax-based code is using it.
ajax
add a comment |Â
up vote
6
down vote
favorite
What could go wrong if I am not using die();
after the function that handles an Ajax request?
I have noticed that almost all the WordPress Ajax-based code is using it.
ajax
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
What could go wrong if I am not using die();
after the function that handles an Ajax request?
I have noticed that almost all the WordPress Ajax-based code is using it.
ajax
What could go wrong if I am not using die();
after the function that handles an Ajax request?
I have noticed that almost all the WordPress Ajax-based code is using it.
ajax
ajax
edited Sep 11 at 7:54
Peter Mortensen
1535
1535
asked Sep 10 at 7:59


Latheesh V M Villa
272114
272114
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
12
down vote
accepted
If you do not die
, execution will continue and might generate extra output which might break whatever information you are trying to send from the server to the browser. Strictly speaking, you might not need to die
, but there is very little point in taking the risk.
In more general terms, WordPress Ajax shows its age and the lack of experience working with Ajax when it was designed. You should use the WordPress JSON routes instead, as they can have a better URL structure, and the programming structure is more logical and modular.
Thank you for answer. A little confused with extra output..if there is no data get from the request,how extra output get generated? Also will it add up the server resources?
– Latheesh V M Villa
Sep 10 at 8:07
because wordpress continues to run and will try to handle the url. If the execution will trigger plugins that do not properly check the context in which their code is being run you might get output (most likely some php error if they expect to be called only in other context)
– Mark Kaplun
Sep 10 at 8:10
1
admin-ajax.php
always adds output if request is not stopped. So, unless the caller of the AJAX ignores the output, thedie
is basically required, even if the wrapperwp_die
should be preferred because a function containingdie
is pretty-much impossible to test.
– gmazzap♦
Sep 10 at 18:19
tnx @gmazzap, been a long while since I looked at the details of that code
– Mark Kaplun
Sep 11 at 3:27
Worth noting that wp_die can also be hooked by other plugins (via wp_die_ajax_handler). Nobody wants to use die or exit, but if you're publishing a plugin there's a risk of conflict if you don't. I even handle my own output buffering due to themes and plugins that produce junk output mid-execution.
– Tim
Sep 11 at 11:07
add a comment |Â
up vote
-1
down vote
WordPress should not require this and it suggests poor coding practices.
When you make an Ajax request, the requested URL should generate a response with the data required (and also set the correct headers to say what type of data it is, e.g. JSON). Nothing else should be provided in the response except the required output.
I work with PHP and JavaScript all the time - although not a WordPress developer - and was amused by the title of this question. If that's what WordPress does, that's awful. As indicated in the accepted answer, it's likely to be a way to stop any further output being included in the response. But of course that "further output" should never be part of the response to begin with.
3
Hi Andy, and welcome here! Regarding your answer, WP best-practice for AJAX is to use admin-ajax.php as the endpoind of the request, and that file literally always adds output if not stopped (see github.com/WordPress/WordPress/blob/master/wp-admin/…) So if you output some JSON from a callback, and don't calldie
(or wrapperwp_die
) WP will add a"0"
that will break the JSON, preventing correct parsing from caller (js probably). So, yes, I agree that in a well developed application one should not be required to die; in WordPress, on the other hand...
– gmazzap♦
Sep 10 at 18:17
@gmazzap yes when i was working with ajax i noticed "0" being added up if don't use wp_die
– Latheesh V M Villa
Sep 10 at 20:03
being "awful" is not a very useful description. there are many awful things in any code which had been around for more than a very trivial time. No code is ever perfect. If you are going to use only perfect code, you will have to write everything yourself starting with the CPU microcode
– Mark Kaplun
Sep 11 at 12:48
I'm trying to educate people by answering the question correctly. Usingdie()
is not required, and never would be in a well-written PHP application. Analogies of CPU code are irrelevant and show a total lack of understanding. Maybe your definition of "awful" and mine are different. I don't use WP because it's littered with nonsense like this. I code (properly) in PHP all the time and am trying to help from a PHP perspective. You can either accept that WP has got this - and many other things - completely wrong. As this is a WP stack I guess the fans of it have a hard time with this.
– Andy
Sep 12 at 10:05
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
accepted
If you do not die
, execution will continue and might generate extra output which might break whatever information you are trying to send from the server to the browser. Strictly speaking, you might not need to die
, but there is very little point in taking the risk.
In more general terms, WordPress Ajax shows its age and the lack of experience working with Ajax when it was designed. You should use the WordPress JSON routes instead, as they can have a better URL structure, and the programming structure is more logical and modular.
Thank you for answer. A little confused with extra output..if there is no data get from the request,how extra output get generated? Also will it add up the server resources?
– Latheesh V M Villa
Sep 10 at 8:07
because wordpress continues to run and will try to handle the url. If the execution will trigger plugins that do not properly check the context in which their code is being run you might get output (most likely some php error if they expect to be called only in other context)
– Mark Kaplun
Sep 10 at 8:10
1
admin-ajax.php
always adds output if request is not stopped. So, unless the caller of the AJAX ignores the output, thedie
is basically required, even if the wrapperwp_die
should be preferred because a function containingdie
is pretty-much impossible to test.
– gmazzap♦
Sep 10 at 18:19
tnx @gmazzap, been a long while since I looked at the details of that code
– Mark Kaplun
Sep 11 at 3:27
Worth noting that wp_die can also be hooked by other plugins (via wp_die_ajax_handler). Nobody wants to use die or exit, but if you're publishing a plugin there's a risk of conflict if you don't. I even handle my own output buffering due to themes and plugins that produce junk output mid-execution.
– Tim
Sep 11 at 11:07
add a comment |Â
up vote
12
down vote
accepted
If you do not die
, execution will continue and might generate extra output which might break whatever information you are trying to send from the server to the browser. Strictly speaking, you might not need to die
, but there is very little point in taking the risk.
In more general terms, WordPress Ajax shows its age and the lack of experience working with Ajax when it was designed. You should use the WordPress JSON routes instead, as they can have a better URL structure, and the programming structure is more logical and modular.
Thank you for answer. A little confused with extra output..if there is no data get from the request,how extra output get generated? Also will it add up the server resources?
– Latheesh V M Villa
Sep 10 at 8:07
because wordpress continues to run and will try to handle the url. If the execution will trigger plugins that do not properly check the context in which their code is being run you might get output (most likely some php error if they expect to be called only in other context)
– Mark Kaplun
Sep 10 at 8:10
1
admin-ajax.php
always adds output if request is not stopped. So, unless the caller of the AJAX ignores the output, thedie
is basically required, even if the wrapperwp_die
should be preferred because a function containingdie
is pretty-much impossible to test.
– gmazzap♦
Sep 10 at 18:19
tnx @gmazzap, been a long while since I looked at the details of that code
– Mark Kaplun
Sep 11 at 3:27
Worth noting that wp_die can also be hooked by other plugins (via wp_die_ajax_handler). Nobody wants to use die or exit, but if you're publishing a plugin there's a risk of conflict if you don't. I even handle my own output buffering due to themes and plugins that produce junk output mid-execution.
– Tim
Sep 11 at 11:07
add a comment |Â
up vote
12
down vote
accepted
up vote
12
down vote
accepted
If you do not die
, execution will continue and might generate extra output which might break whatever information you are trying to send from the server to the browser. Strictly speaking, you might not need to die
, but there is very little point in taking the risk.
In more general terms, WordPress Ajax shows its age and the lack of experience working with Ajax when it was designed. You should use the WordPress JSON routes instead, as they can have a better URL structure, and the programming structure is more logical and modular.
If you do not die
, execution will continue and might generate extra output which might break whatever information you are trying to send from the server to the browser. Strictly speaking, you might not need to die
, but there is very little point in taking the risk.
In more general terms, WordPress Ajax shows its age and the lack of experience working with Ajax when it was designed. You should use the WordPress JSON routes instead, as they can have a better URL structure, and the programming structure is more logical and modular.
edited Sep 11 at 7:54
Peter Mortensen
1535
1535
answered Sep 10 at 8:02
Mark Kaplun
19.9k52654
19.9k52654
Thank you for answer. A little confused with extra output..if there is no data get from the request,how extra output get generated? Also will it add up the server resources?
– Latheesh V M Villa
Sep 10 at 8:07
because wordpress continues to run and will try to handle the url. If the execution will trigger plugins that do not properly check the context in which their code is being run you might get output (most likely some php error if they expect to be called only in other context)
– Mark Kaplun
Sep 10 at 8:10
1
admin-ajax.php
always adds output if request is not stopped. So, unless the caller of the AJAX ignores the output, thedie
is basically required, even if the wrapperwp_die
should be preferred because a function containingdie
is pretty-much impossible to test.
– gmazzap♦
Sep 10 at 18:19
tnx @gmazzap, been a long while since I looked at the details of that code
– Mark Kaplun
Sep 11 at 3:27
Worth noting that wp_die can also be hooked by other plugins (via wp_die_ajax_handler). Nobody wants to use die or exit, but if you're publishing a plugin there's a risk of conflict if you don't. I even handle my own output buffering due to themes and plugins that produce junk output mid-execution.
– Tim
Sep 11 at 11:07
add a comment |Â
Thank you for answer. A little confused with extra output..if there is no data get from the request,how extra output get generated? Also will it add up the server resources?
– Latheesh V M Villa
Sep 10 at 8:07
because wordpress continues to run and will try to handle the url. If the execution will trigger plugins that do not properly check the context in which their code is being run you might get output (most likely some php error if they expect to be called only in other context)
– Mark Kaplun
Sep 10 at 8:10
1
admin-ajax.php
always adds output if request is not stopped. So, unless the caller of the AJAX ignores the output, thedie
is basically required, even if the wrapperwp_die
should be preferred because a function containingdie
is pretty-much impossible to test.
– gmazzap♦
Sep 10 at 18:19
tnx @gmazzap, been a long while since I looked at the details of that code
– Mark Kaplun
Sep 11 at 3:27
Worth noting that wp_die can also be hooked by other plugins (via wp_die_ajax_handler). Nobody wants to use die or exit, but if you're publishing a plugin there's a risk of conflict if you don't. I even handle my own output buffering due to themes and plugins that produce junk output mid-execution.
– Tim
Sep 11 at 11:07
Thank you for answer. A little confused with extra output..if there is no data get from the request,how extra output get generated? Also will it add up the server resources?
– Latheesh V M Villa
Sep 10 at 8:07
Thank you for answer. A little confused with extra output..if there is no data get from the request,how extra output get generated? Also will it add up the server resources?
– Latheesh V M Villa
Sep 10 at 8:07
because wordpress continues to run and will try to handle the url. If the execution will trigger plugins that do not properly check the context in which their code is being run you might get output (most likely some php error if they expect to be called only in other context)
– Mark Kaplun
Sep 10 at 8:10
because wordpress continues to run and will try to handle the url. If the execution will trigger plugins that do not properly check the context in which their code is being run you might get output (most likely some php error if they expect to be called only in other context)
– Mark Kaplun
Sep 10 at 8:10
1
1
admin-ajax.php
always adds output if request is not stopped. So, unless the caller of the AJAX ignores the output, the die
is basically required, even if the wrapper wp_die
should be preferred because a function containing die
is pretty-much impossible to test.– gmazzap♦
Sep 10 at 18:19
admin-ajax.php
always adds output if request is not stopped. So, unless the caller of the AJAX ignores the output, the die
is basically required, even if the wrapper wp_die
should be preferred because a function containing die
is pretty-much impossible to test.– gmazzap♦
Sep 10 at 18:19
tnx @gmazzap, been a long while since I looked at the details of that code
– Mark Kaplun
Sep 11 at 3:27
tnx @gmazzap, been a long while since I looked at the details of that code
– Mark Kaplun
Sep 11 at 3:27
Worth noting that wp_die can also be hooked by other plugins (via wp_die_ajax_handler). Nobody wants to use die or exit, but if you're publishing a plugin there's a risk of conflict if you don't. I even handle my own output buffering due to themes and plugins that produce junk output mid-execution.
– Tim
Sep 11 at 11:07
Worth noting that wp_die can also be hooked by other plugins (via wp_die_ajax_handler). Nobody wants to use die or exit, but if you're publishing a plugin there's a risk of conflict if you don't. I even handle my own output buffering due to themes and plugins that produce junk output mid-execution.
– Tim
Sep 11 at 11:07
add a comment |Â
up vote
-1
down vote
WordPress should not require this and it suggests poor coding practices.
When you make an Ajax request, the requested URL should generate a response with the data required (and also set the correct headers to say what type of data it is, e.g. JSON). Nothing else should be provided in the response except the required output.
I work with PHP and JavaScript all the time - although not a WordPress developer - and was amused by the title of this question. If that's what WordPress does, that's awful. As indicated in the accepted answer, it's likely to be a way to stop any further output being included in the response. But of course that "further output" should never be part of the response to begin with.
3
Hi Andy, and welcome here! Regarding your answer, WP best-practice for AJAX is to use admin-ajax.php as the endpoind of the request, and that file literally always adds output if not stopped (see github.com/WordPress/WordPress/blob/master/wp-admin/…) So if you output some JSON from a callback, and don't calldie
(or wrapperwp_die
) WP will add a"0"
that will break the JSON, preventing correct parsing from caller (js probably). So, yes, I agree that in a well developed application one should not be required to die; in WordPress, on the other hand...
– gmazzap♦
Sep 10 at 18:17
@gmazzap yes when i was working with ajax i noticed "0" being added up if don't use wp_die
– Latheesh V M Villa
Sep 10 at 20:03
being "awful" is not a very useful description. there are many awful things in any code which had been around for more than a very trivial time. No code is ever perfect. If you are going to use only perfect code, you will have to write everything yourself starting with the CPU microcode
– Mark Kaplun
Sep 11 at 12:48
I'm trying to educate people by answering the question correctly. Usingdie()
is not required, and never would be in a well-written PHP application. Analogies of CPU code are irrelevant and show a total lack of understanding. Maybe your definition of "awful" and mine are different. I don't use WP because it's littered with nonsense like this. I code (properly) in PHP all the time and am trying to help from a PHP perspective. You can either accept that WP has got this - and many other things - completely wrong. As this is a WP stack I guess the fans of it have a hard time with this.
– Andy
Sep 12 at 10:05
add a comment |Â
up vote
-1
down vote
WordPress should not require this and it suggests poor coding practices.
When you make an Ajax request, the requested URL should generate a response with the data required (and also set the correct headers to say what type of data it is, e.g. JSON). Nothing else should be provided in the response except the required output.
I work with PHP and JavaScript all the time - although not a WordPress developer - and was amused by the title of this question. If that's what WordPress does, that's awful. As indicated in the accepted answer, it's likely to be a way to stop any further output being included in the response. But of course that "further output" should never be part of the response to begin with.
3
Hi Andy, and welcome here! Regarding your answer, WP best-practice for AJAX is to use admin-ajax.php as the endpoind of the request, and that file literally always adds output if not stopped (see github.com/WordPress/WordPress/blob/master/wp-admin/…) So if you output some JSON from a callback, and don't calldie
(or wrapperwp_die
) WP will add a"0"
that will break the JSON, preventing correct parsing from caller (js probably). So, yes, I agree that in a well developed application one should not be required to die; in WordPress, on the other hand...
– gmazzap♦
Sep 10 at 18:17
@gmazzap yes when i was working with ajax i noticed "0" being added up if don't use wp_die
– Latheesh V M Villa
Sep 10 at 20:03
being "awful" is not a very useful description. there are many awful things in any code which had been around for more than a very trivial time. No code is ever perfect. If you are going to use only perfect code, you will have to write everything yourself starting with the CPU microcode
– Mark Kaplun
Sep 11 at 12:48
I'm trying to educate people by answering the question correctly. Usingdie()
is not required, and never would be in a well-written PHP application. Analogies of CPU code are irrelevant and show a total lack of understanding. Maybe your definition of "awful" and mine are different. I don't use WP because it's littered with nonsense like this. I code (properly) in PHP all the time and am trying to help from a PHP perspective. You can either accept that WP has got this - and many other things - completely wrong. As this is a WP stack I guess the fans of it have a hard time with this.
– Andy
Sep 12 at 10:05
add a comment |Â
up vote
-1
down vote
up vote
-1
down vote
WordPress should not require this and it suggests poor coding practices.
When you make an Ajax request, the requested URL should generate a response with the data required (and also set the correct headers to say what type of data it is, e.g. JSON). Nothing else should be provided in the response except the required output.
I work with PHP and JavaScript all the time - although not a WordPress developer - and was amused by the title of this question. If that's what WordPress does, that's awful. As indicated in the accepted answer, it's likely to be a way to stop any further output being included in the response. But of course that "further output" should never be part of the response to begin with.
WordPress should not require this and it suggests poor coding practices.
When you make an Ajax request, the requested URL should generate a response with the data required (and also set the correct headers to say what type of data it is, e.g. JSON). Nothing else should be provided in the response except the required output.
I work with PHP and JavaScript all the time - although not a WordPress developer - and was amused by the title of this question. If that's what WordPress does, that's awful. As indicated in the accepted answer, it's likely to be a way to stop any further output being included in the response. But of course that "further output" should never be part of the response to begin with.
edited Sep 11 at 7:54
Peter Mortensen
1535
1535
answered Sep 10 at 14:40
Andy
1153
1153
3
Hi Andy, and welcome here! Regarding your answer, WP best-practice for AJAX is to use admin-ajax.php as the endpoind of the request, and that file literally always adds output if not stopped (see github.com/WordPress/WordPress/blob/master/wp-admin/…) So if you output some JSON from a callback, and don't calldie
(or wrapperwp_die
) WP will add a"0"
that will break the JSON, preventing correct parsing from caller (js probably). So, yes, I agree that in a well developed application one should not be required to die; in WordPress, on the other hand...
– gmazzap♦
Sep 10 at 18:17
@gmazzap yes when i was working with ajax i noticed "0" being added up if don't use wp_die
– Latheesh V M Villa
Sep 10 at 20:03
being "awful" is not a very useful description. there are many awful things in any code which had been around for more than a very trivial time. No code is ever perfect. If you are going to use only perfect code, you will have to write everything yourself starting with the CPU microcode
– Mark Kaplun
Sep 11 at 12:48
I'm trying to educate people by answering the question correctly. Usingdie()
is not required, and never would be in a well-written PHP application. Analogies of CPU code are irrelevant and show a total lack of understanding. Maybe your definition of "awful" and mine are different. I don't use WP because it's littered with nonsense like this. I code (properly) in PHP all the time and am trying to help from a PHP perspective. You can either accept that WP has got this - and many other things - completely wrong. As this is a WP stack I guess the fans of it have a hard time with this.
– Andy
Sep 12 at 10:05
add a comment |Â
3
Hi Andy, and welcome here! Regarding your answer, WP best-practice for AJAX is to use admin-ajax.php as the endpoind of the request, and that file literally always adds output if not stopped (see github.com/WordPress/WordPress/blob/master/wp-admin/…) So if you output some JSON from a callback, and don't calldie
(or wrapperwp_die
) WP will add a"0"
that will break the JSON, preventing correct parsing from caller (js probably). So, yes, I agree that in a well developed application one should not be required to die; in WordPress, on the other hand...
– gmazzap♦
Sep 10 at 18:17
@gmazzap yes when i was working with ajax i noticed "0" being added up if don't use wp_die
– Latheesh V M Villa
Sep 10 at 20:03
being "awful" is not a very useful description. there are many awful things in any code which had been around for more than a very trivial time. No code is ever perfect. If you are going to use only perfect code, you will have to write everything yourself starting with the CPU microcode
– Mark Kaplun
Sep 11 at 12:48
I'm trying to educate people by answering the question correctly. Usingdie()
is not required, and never would be in a well-written PHP application. Analogies of CPU code are irrelevant and show a total lack of understanding. Maybe your definition of "awful" and mine are different. I don't use WP because it's littered with nonsense like this. I code (properly) in PHP all the time and am trying to help from a PHP perspective. You can either accept that WP has got this - and many other things - completely wrong. As this is a WP stack I guess the fans of it have a hard time with this.
– Andy
Sep 12 at 10:05
3
3
Hi Andy, and welcome here! Regarding your answer, WP best-practice for AJAX is to use admin-ajax.php as the endpoind of the request, and that file literally always adds output if not stopped (see github.com/WordPress/WordPress/blob/master/wp-admin/…) So if you output some JSON from a callback, and don't call
die
(or wrapper wp_die
) WP will add a "0"
that will break the JSON, preventing correct parsing from caller (js probably). So, yes, I agree that in a well developed application one should not be required to die; in WordPress, on the other hand...– gmazzap♦
Sep 10 at 18:17
Hi Andy, and welcome here! Regarding your answer, WP best-practice for AJAX is to use admin-ajax.php as the endpoind of the request, and that file literally always adds output if not stopped (see github.com/WordPress/WordPress/blob/master/wp-admin/…) So if you output some JSON from a callback, and don't call
die
(or wrapper wp_die
) WP will add a "0"
that will break the JSON, preventing correct parsing from caller (js probably). So, yes, I agree that in a well developed application one should not be required to die; in WordPress, on the other hand...– gmazzap♦
Sep 10 at 18:17
@gmazzap yes when i was working with ajax i noticed "0" being added up if don't use wp_die
– Latheesh V M Villa
Sep 10 at 20:03
@gmazzap yes when i was working with ajax i noticed "0" being added up if don't use wp_die
– Latheesh V M Villa
Sep 10 at 20:03
being "awful" is not a very useful description. there are many awful things in any code which had been around for more than a very trivial time. No code is ever perfect. If you are going to use only perfect code, you will have to write everything yourself starting with the CPU microcode
– Mark Kaplun
Sep 11 at 12:48
being "awful" is not a very useful description. there are many awful things in any code which had been around for more than a very trivial time. No code is ever perfect. If you are going to use only perfect code, you will have to write everything yourself starting with the CPU microcode
– Mark Kaplun
Sep 11 at 12:48
I'm trying to educate people by answering the question correctly. Using
die()
is not required, and never would be in a well-written PHP application. Analogies of CPU code are irrelevant and show a total lack of understanding. Maybe your definition of "awful" and mine are different. I don't use WP because it's littered with nonsense like this. I code (properly) in PHP all the time and am trying to help from a PHP perspective. You can either accept that WP has got this - and many other things - completely wrong. As this is a WP stack I guess the fans of it have a hard time with this.– Andy
Sep 12 at 10:05
I'm trying to educate people by answering the question correctly. Using
die()
is not required, and never would be in a well-written PHP application. Analogies of CPU code are irrelevant and show a total lack of understanding. Maybe your definition of "awful" and mine are different. I don't use WP because it's littered with nonsense like this. I code (properly) in PHP all the time and am trying to help from a PHP perspective. You can either accept that WP has got this - and many other things - completely wrong. As this is a WP stack I guess the fans of it have a hard time with this.– Andy
Sep 12 at 10:05
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f313779%2fwhy-is-die-used-at-the-end-of-function-that-handles-an-ajax-request%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password