返回介绍

settings_errors()

发布于 2017-09-11 10:21:25 字数 3467 浏览 1161 评论 0 收藏 0

settings_errors( string $setting = '',  bool $sanitize = false,  bool $hide_on_update = false )

Display settings errors registered by add_settings_error().


description

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.


参数

$setting

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

Default value: ''

$sanitize

(bool) (Optional) Whether to re-sanitize the setting value before returning errors.

Default value: false

$hide_on_update

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

Default value: false


源代码

File: wp-admin/includes/template.php

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 .= "<div id='$css_id' class='$css_class'> \n";
		$output .= "<p><strong>{$details['message']}</strong></p>";
		$output .= "</div> \n";
	}
	echo $output;
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-admin/includes/template.php: get_settings_errors()

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

    Example

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

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

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

发布评论

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