Home » Functions » add_settings_error

WordPress is a platform that provides a lot of customization options to its users. In order to maintain the consistency of the settings and the user’s experience, WordPress provides the add_settings_error function. This function can be used to add a new error message to the WordPress settings page.

The add_settings_error function takes four parameters. The first parameter is the settings page slug, which is a unique identifier for the settings page. The second parameter is the error code, which is used to identify the error message. The third parameter is the error message itself, which is displayed to the user. The fourth parameter is the error type, which can be either "error" or "updated". This parameter is used to determine the severity of the error message.

This function is useful for developers who are creating custom WordPress plugins or themes. It allows them to add custom error messages to the WordPress settings page, which can help users understand the nature of the error and how to fix it.

Example usage code:

function my_plugin_settings_page() {
    add_settings_error( 'my_plugin_settings', 'my_plugin_error', 'An error occurred while saving your settings.', 'error' );
add_action( 'admin_notices', 'my_plugin_settings_page' );

In this example, we have created a custom settings page for our plugin, and added an error message using the add_settings_error function. The error message will be displayed in the WordPress admin area when the user saves their settings and an error occurs.

Learn More on

Register an account to save your snippets or go Pro to get more features.