나는이 답변을 받아들이지 않을 것이며, 이것은 공평하지 않을 것이다. 중요한 항목에 대한 개요와 가능한 힌트를 만들고 싶었습니다.
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: }
일반적으로 아약스 호출에서 얻는 것은 일종의 응답입니다. 응답은 json으로 인코딩되거나 json으로 인코딩되지 않을 수 있습니다.
우리가 json
outupt 가 필요 wp_send_json
하거나 두 개의 위성이 좋은 생각입니다.
그러나, 우리는 반환 할 수 있습니다 x-www-form-urlencoded
또는 multipart/form-data
또는 text/xml
또는 기타 인코딩 유형. 이 경우을 사용하지 않습니다 wp_send_json
.
전체 HTML을 반환 할 수 있으며이 경우 wp_die()
첫 번째와 두 번째 매개 변수 를 사용하는 것이 좋습니다. 그렇지 않으면 이러한 매개 변수가 비어 있어야합니다.
wp_die( '', '', array(
'response' => null,
) );
그러나 wp_die()
매개 변수없이 호출하면 어떤 이점이 있습니까?
마지막으로, 훌륭한 WP 코어를 확인하면
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();
두 형식을 사용 die()
하고 wp_die()
. 이유를 설명 할 수 있습니까?
마지막으로 여기에 admin-ajax.php
반환되는 것이 있습니다.die( '0' );
왜 안돼 wp_die(...)
?