我不会接受这个答案,这不公平。我只想在我认为重要的项目上创建轮廓和可能的提示:
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: }
通常,我们从ajax调用中得到的是某种响应。响应可能使用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(...)
呢?