返回介绍

wp_die()

发布于 2017-09-11 11:53:49 字数 12637 浏览 1112 评论 0 收藏 0

wp_die( string|WP_Error $message = '',  string|int $title = '',  string|array|int $args = array() )

Kill WordPress execution and display HTML message with error message.


description

This function complements the die() PHP function. The difference is that HTML will be displayed to the user. It is recommended to use this function only when the execution should not continue any further. It is not recommended to call this function very often, and try to handle as many errors as possible silently or more gracefully.

As a shorthand, the desired HTTP response code may be passed as an integer to the $title parameter (the default title would apply) or the $args parameter.


参数

$message

(string|WP_Error) (Optional) Error message. If this is a WP_Error object, and not an Ajax or XML-RPC request, the error's messages are used.

Default value: ''

$title

(string|int) (Optional) Error title. If $message is a WP_Error object, error data with the key 'title' may be used to specify the title. If $title is an integer, then it is treated as the response code.

Default value: ''

$args

(string|array|int) (Optional) Arguments to control behavior. If $args is an integer, then it is treated as the response code.

  • 'response'
    (int) The HTTP response code. Default 200 for Ajax requests, 500 otherwise.
  • 'back_link'
    (bool) Whether to include a link to go back. Default false.
  • 'text_direction'
    (string) The text direction. This is only useful internally, when WordPress is still loading and the site's locale is not set up yet. Accepts 'rtl'. Default is the value of is_rtl().

Default value: array()


源代码

File: wp-includes/functions.php

function wp_die( $message = '', $title = '', $args = array() ) {

	if ( is_int( $args ) ) {
		$args = array( 'response' => $args );
	} elseif ( is_int( $title ) ) {
		$args  = array( 'response' => $title );
		$title = '';
	}

	if ( wp_doing_ajax() ) {
		/**
		 * Filters the callback for killing WordPress execution for Ajax requests.
		 *
		 * @since 3.4.0
		 *
		 * @param callable $function Callback function name.
		 */
		$function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
	} elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
		/**
		 * Filters the callback for killing WordPress execution for XML-RPC requests.
		 *
		 * @since 3.4.0
		 *
		 * @param callable $function Callback function name.
		 */
		$function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
	} else {
		/**
		 * Filters the callback for killing WordPress execution for all non-Ajax, non-XML-RPC requests.
		 *
		 * @since 3.0.0
		 *
		 * @param callable $function Callback function name.
		 */
		$function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
	}

	call_user_func( $function, $message, $title, $args );
}

更新日志

Versiondescription
4.1.0The $title and $args parameters were changed to optionally accept an integer to be used as the response code.
2.0.4Introduced.

相关函数

Uses

  • wp-includes/load.php: wp_doing_ajax()
  • wp-includes/functions.php: wp_die_ajax_handler
  • wp-includes/functions.php: wp_die_xmlrpc_handler
  • wp-includes/functions.php: wp_die_handler
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-includes/comment.php: wp_check_comment_flood()
  • wp-admin/includes/ajax-actions.php: wp_ajax_get_post_thumbnail_html()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_inactive_widgets()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::ajax_load_available_items()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::ajax_search_available_items()
  • wp-admin/includes/media.php: wp_media_attach_action()
  • wp-login.php: retrieve_password()
  • wp-admin/includes/class-file-upload-upgrader.php: File_Upload_Upgrader::__construct()
  • wp-admin/includes/theme-install.php: install_theme_information()
  • wp-admin/includes/ms.php: _access_denied_splash()
  • wp-admin/includes/ms.php: check_upload_size()
  • wp-admin/includes/class-wp-theme-install-list-table.php: WP_Theme_Install_List_Table::prepare_items()
  • wp-admin/includes/plugin-install.php: install_plugin_information()
  • wp-admin/includes/user.php: edit_user()
  • wp-admin/includes/post.php: post_preview()
  • wp-admin/includes/post.php: write_post()
  • wp-admin/includes/post.php: edit_post()
  • wp-admin/includes/post.php: bulk_edit_posts()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_widget()
  • wp-admin/includes/ajax-actions.php: wp_ajax_upload_attachment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_image_editor()
  • wp-admin/includes/ajax-actions.php: wp_ajax_set_post_thumbnail()
  • wp-admin/includes/ajax-actions.php: wp_ajax_date_format()
  • wp-admin/includes/ajax-actions.php: wp_ajax_time_format()
  • wp-admin/includes/ajax-actions.php: wp_ajax_wp_remove_post_lock()
  • wp-admin/includes/ajax-actions.php: wp_ajax_dismiss_wp_pointer()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_menu_item()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_meta()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_user()
  • wp-admin/includes/ajax-actions.php: wp_ajax_closed_postboxes()
  • wp-admin/includes/ajax-actions.php: wp_ajax_hidden_columns()
  • wp-admin/includes/ajax-actions.php: wp_ajax_update_welcome_panel()
  • wp-admin/includes/ajax-actions.php: wp_ajax_menu_get_metabox()
  • wp-admin/includes/ajax-actions.php: wp_ajax_wp_link_ajax()
  • wp-admin/includes/ajax-actions.php: wp_ajax_menu_locations_save()
  • wp-admin/includes/ajax-actions.php: wp_ajax_meta_box_order()
  • wp-admin/includes/ajax-actions.php: wp_ajax_menu_quick_search()
  • wp-admin/includes/ajax-actions.php: wp_ajax_get_permalink()
  • wp-admin/includes/ajax-actions.php: wp_ajax_sample_permalink()
  • wp-admin/includes/ajax-actions.php: wp_ajax_inline_save()
  • wp-admin/includes/ajax-actions.php: wp_ajax_inline_save_tax()
  • wp-admin/includes/ajax-actions.php: wp_ajax_widgets_order()
  • wp-admin/includes/ajax-actions.php: wp_ajax_dashboard_widgets()
  • wp-admin/includes/ajax-actions.php: wp_ajax_logged_in()
  • wp-admin/includes/ajax-actions.php: _wp_ajax_add_hierarchical_term()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_comment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_tag()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_link()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_meta()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_post()
  • wp-admin/includes/ajax-actions.php: wp_ajax_trash_post()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_page()
  • wp-admin/includes/ajax-actions.php: wp_ajax_dim_comment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_link_category()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_tag()
  • wp-admin/includes/ajax-actions.php: wp_ajax_get_tagcloud()
  • wp-admin/includes/ajax-actions.php: wp_ajax_get_comments()
  • wp-admin/includes/ajax-actions.php: wp_ajax_replyto_comment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_edit_comment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_fetch_list()
  • wp-admin/includes/ajax-actions.php: wp_ajax_ajax_tag_search()
  • wp-admin/includes/ajax-actions.php: wp_ajax_wp_compression_test()
  • wp-admin/includes/ajax-actions.php: wp_ajax_imgedit_preview()
  • wp-admin/includes/ajax-actions.php: wp_ajax_oembed_cache()
  • wp-admin/includes/ajax-actions.php: wp_ajax_autocomplete_user()
  • wp-admin/includes/bookmark.php: wp_link_manager_disabled_message()
  • wp-admin/includes/bookmark.php: edit_link()
  • wp-admin/includes/ms-deprecated.php: wpmu_checkAvailableSpace()
  • wp-admin/includes/class-wp-terms-list-table.php: WP_Terms_List_Table::__construct()
  • wp-admin/includes/file.php: validate_file_to_edit()
  • wp-admin/includes/comment.php: edit_comment()
  • wp-admin/custom-header.php: Custom_Image_Header::step_2()
  • wp-admin/custom-header.php: Custom_Image_Header::step_2_manage_upload()
  • wp-admin/custom-header.php: Custom_Image_Header::step_3()
  • wp-admin/custom-header.php: Custom_Image_Header::admin_page()
  • wp-admin/includes/ms.php: confirm_delete_users()
  • wp-admin/custom-background.php: Custom_Background::handle_upload()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::wp_die()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::setup_theme()
  • wp-includes/pluggable.php: check_ajax_referer()
  • wp-includes/load.php: wp_set_wpdb_vars()
  • wp-includes/load.php: wp_not_installed()
  • wp-includes/functions.php: wp_send_json()
  • wp-includes/functions.php: dead_db()
  • wp-includes/functions.php: wp_nonce_ays()
  • wp-includes/functions.php: do_feed()
  • wp-includes/class-wp-ajax-response.php: WP_Ajax_Response::send()
  • wp-includes/option.php: wp_protect_special_option()
  • wp-includes/revision.php: _show_post_preview()
  • wp-includes/ms-functions.php: maybe_add_existing_user_to_blog()
  • wp-includes/ms-functions.php: signup_nonce_check()
  • wp-includes/ms-load.php: ms_not_installed()
  • wp-includes/ms-load.php: ms_site_check()
  • wp-includes/wp-db.php: wpdb::bail()
  • wp-includes/wp-db.php: wpdb::print_error()
  • wp-includes/class-wp-customize-widgets.php: WP_Customize_Widgets::wp_ajax_update_widget()
  • wp-includes/comment.php: wp_allow_comment()
  • Show 92 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Test to see what is in the $post variable in a filter:

    
    /**
     * Add new body class.
     *
     * Testing what is in the $post variable.
     *
     * @param array $classes Body classes.
     */
    function wpdocs_add_body_class( $classes ) {
    	/** @global WP_Post $post */
    	global $post;
    
    	wp_die( '<pre>' . var_export( $post, true ) . '</pre>' );
    }
    add_filter( 'body_class', 'wpdocs_add_body_class' );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文