Je n'accepterai pas cette réponse, ce ne serait pas juste. Je voulais juste créer un plan et des conseils possibles sur les éléments que je trouve importants:
La définition principale de wp-die ()
File: wp-includes/functions.php
2607: /**
2608: * Kill WordPress execution and display HTML message with error message.
2609: *
2610: * This function complements the `die()` PHP function. The difference is that
2611: * HTML will be displayed to the user. It is recommended to use this function
2612: * only when the execution should not continue any further. It is not recommended
2613: * to call this function very often, and try to handle as many errors as possible
2614: * silently or more gracefully.
2615: *
2616: * As a shorthand, the desired HTTP response code may be passed as an integer to
2617: * the `$title` parameter (the default title would apply) or the `$args` parameter.
2618: *
2619: * @since 2.0.4
2620: * @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept
2621: * an integer to be used as the response code.
2622: *
2623: * @param string|WP_Error $message Optional. Error message. If this is a WP_Error object,
2624: * and not an Ajax or XML-RPC request, the error's messages are used.
2625: * Default empty.
2626: * @param string|int $title Optional. Error title. If `$message` is a `WP_Error` object,
2627: * error data with the key 'title' may be used to specify the title.
2628: * If `$title` is an integer, then it is treated as the response
2629: * code. Default empty.
2630: * @param string|array|int $args {
2631: * Optional. Arguments to control behavior. If `$args` is an integer, then it is treated
2632: * as the response code. Default empty array.
2633: *
2634: * @type int $response The HTTP response code. Default 200 for Ajax requests, 500 otherwise.
2635: * @type bool $back_link Whether to include a link to go back. Default false.
2636: * @type string $text_direction The text direction. This is only useful internally, when WordPress
2637: * is still loading and the site's locale is not set up yet. Accepts 'rtl'.
2638: * Default is the value of is_rtl().
2639: * }
2640: */
2641: function wp_die( $message = '', $title = '', $args = array() ) {
2642:
2643: if ( is_int( $args ) ) {
2644: $args = array( 'response' => $args );
2645: } elseif ( is_int( $title ) ) {
2646: $args = array( 'response' => $title );
2647: $title = '';
2648: }
2649:
2650: if ( wp_doing_ajax() ) {
2651: /**
2652: * Filters the callback for killing WordPress execution for Ajax requests.
2653: *
2654: * @since 3.4.0
2655: *
2656: * @param callable $function Callback function name.
2657: */
2658: $function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
2659: } elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
2660: /**
2661: * Filters the callback for killing WordPress execution for XML-RPC requests.
2662: *
2663: * @since 3.4.0
2664: *
2665: * @param callable $function Callback function name.
2666: */
2667: $function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
2668: } else {
2669: /**
2670: * Filters the callback for killing WordPress execution for all non-Ajax, non-XML-RPC requests.
2671: *
2672: * @since 3.0.0
2673: *
2674: * @param callable $function Callback function name.
2675: */
2676: $function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
2677: }
2678:
2679: call_user_func( $function, $message, $title, $args );
2680: }
wp_send_json
File: wp-includes/functions.php
3144: /**
3145: * Send a JSON response back to an Ajax request.
3146: *
3147: * @since 3.5.0
3148: * @since 4.7.0 The `$status_code` parameter was added.
3149: *
3150: * @param mixed $response Variable (usually an array or object) to encode as JSON,
3151: * then print and die.
3152: * @param int $status_code The HTTP status code to output.
3153: */
3154: function wp_send_json( $response, $status_code = null ) {
3155: @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
3156: if ( null !== $status_code ) {
3157: status_header( $status_code );
3158: }
3159: echo wp_json_encode( $response );
3160:
3161: if ( wp_doing_ajax() ) {
3162: wp_die( '', '', array(
3163: 'response' => null,
3164: ) );
3165: } else {
3166: die;
3167: }
3168: }
wp_doing_ajax
File: wp-includes/load.php
1044: /**
1045: * Determines whether the current request is a WordPress Ajax request.
1046: *
1047: * @since 4.7.0
1048: *
1049: * @return bool True if it's a WordPress Ajax request, false otherwise.
1050: */
1051: function wp_doing_ajax() {
1052: /**
1053: * Filters whether the current request is a WordPress Ajax request.
1054: *
1055: * @since 4.7.0
1056: *
1057: * @param bool $wp_doing_ajax Whether the current request is a WordPress Ajax request.
1058: */
1059: return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX );
1060: }
Généralement, ce que nous obtenons de l'appel ajax est une sorte de réponse. La réponse peut être codée en json ou ne pas être codée en json.
Au cas où nous json
aurions besoin d'une interruption wp_send_json
ou deux satellites sont une excellente idée.
Cependant, nous pouvons retourner x-www-form-urlencoded
ou multipart/form-data
ou text/xml
ou tout autre type d'encodage. Dans ce cas, nous n'utilisons pas wp_send_json
.
Nous pouvons renvoyer l'intégralité du code HTML et dans ce cas, il est judicieux d'utiliser les wp_die()
premier et deuxième paramètres, sinon ces paramètres doivent être vides.
wp_die( '', '', array(
'response' => null,
) );
Mais quel est l'avantage d'appeler wp_die()
sans paramètres?
Enfin, si vous vérifiez le grand noyau WP que vous pouvez trouver
File: wp-includes/class-wp-ajax-response.php
139: /**
140: * Display XML formatted responses.
141: *
142: * Sets the content type header to text/xml.
143: *
144: * @since 2.1.0
145: */
146: public function send() {
147: header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) );
148: echo "<?xml version='1.0' encoding='" . get_option( 'blog_charset' ) . "' standalone='yes'?><wp_ajax>";
149: foreach ( (array) $this->responses as $response )
150: echo $response;
151: echo '</wp_ajax>';
152: if ( wp_doing_ajax() )
153: wp_die();
154: else
155: die();
Les deux formats sont utilisés die()
et wp_die()
. Pouvez-vous expliquer pourquoi?
Enfin voici ce qui admin-ajax.php
revientdie( '0' );
Pourquoi pas wp_die(...)
?