WordPress at a glance

settings_errors() WP 1.0

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.

Works based on: get_settings_errors()

No Hooks.

Return

Nothing (null).

Usage

settings_errors( $setting, $sanitize, $hide_on_update );
$setting(string)
Optional slug title of a specific setting who's errors you want.
Default: ''
$sanitize(true/false)
Whether to re-sanitize the setting value before returning errors.
Default: false
$hide_on_update(true/false)
If set to true errors will not be shown if the settings page has already been submitted.
Default: false

Code of settings_errors: wp-admin/includes/template.php VER 4.9.8

<?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;
}

Related Functions

From category: Settings API

No comments
    Hello, !     Log In . Register