settings_errors()显示所有已经注册的设置选项错误提示信息

目录

描述

Display settings errors registered by add_settings_error().

Part of the Settings API. Outputs a <div> for each error retrieved by get_settings_errors().

This is called automatically after a settings page based on the Settings API is submitted. Errors should be added during the validation callback function for a setting defined in register_setting().

The $sanitize option is passed into get_settings_errors() and will re-run the setting sanitization on its current value.

The $hide_on_update option will cause errors to only show when the settings page is first loaded.

If the user has already saved new values it will be hidden to avoid repeating messages already shown in the default error reporting after submission. This is useful to show general errors like missing settings when the user arrives at the settings page.

settings_errors() 描述

用法

<?php settings_errors$setting$sanitize$hide_on_update ?>

settings_errors() 用法

参数

$setting

(string) (可选) Optional slug title of a specific setting who's errors you want.

默认值: None

$sanitize

(boolean) (可选) Whether to re-sanitize the setting value before returning errors.

默认值: false

$hide_on_update

(boolean) (可选) If set to true errors will not be shown if the settings page has already been submitted.

默认值: false

settings_errors() 参数

返回值

(void) 

This function does not return a value.

settings_errors() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

/**
 * Displays all messages registered to 'your-settings-error-slug'
 */
function your_admin_notices_action() {
    settings_errors( 'your-settings-error-slug' );
}
add_action( 'admin_notices', 'your_admin_notices_action' );

settings_errors() 示例

历史

添加于 版本: 3.0

settings_errors() 历史

源文件

settings_errors() 函数的代码位于 wp-admin/includes/template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Display settings errors registered by {@see add_settings_error()}.
 *
 * Part of the Settings API. Outputs a div for each error retrieved by
 * {@see get_settings_errors()}.
 *
 * This is called automatically after a settings page based on the
 * Settings API is submitted. Errors should be added during the validation
 * callback function for a setting defined in {@see register_setting()}
 *
 * The $sanitize option is passed into {@see get_settings_errors()} and will
 * re-run the setting sanitization
 * on its current value.
 *
 * The $hide_on_update option will cause errors to only show when the settings
 * page is first loaded. if the user has already saved new values it will be
 * hidden to avoid repeating messages already shown in the default error
 * reporting after submission. This is useful to show general errors like
 * missing settings when the user arrives at the settings page.
 *
 * @since 3.0.0
 *
 * @param string $setting        Optional slug title of a specific setting who's errors you want.
 * @param bool   $sanitize       Whether to re-sanitize the setting value before returning errors.
 * @param bool   $hide_on_update If set to true errors will not be shown if the settings page has already been submitted.
 */
function settings_errors( $setting = '', $sanitize = false, $hide_on_update = false ) {

	if ( $hide_on_update && ! empty( $_GET['settings-updated'] ) )
		return;

	$settings_errors = get_settings_errors( $setting, $sanitize );

	if ( empty( $settings_errors ) )
		return;

	$output = '';
	foreach ( $settings_errors as $key => $details ) {
		$css_id = 'setting-error-' . $details['code'];
		$css_class = $details['type'] . ' settings-error notice is-dismissible';
		$output .= "
"; $output .= "

{$details['message']}

"; $output .= "
"; } echo $output; }

settings_errors() 源文件

相关