How to Display Notices and Errors using the Settings API

As we all know, the Settings API is fantastic for building plugin and theme options and settings pages.

There are countless tutorials about it, and we also highlighted some of the best ones in a recent post.

A plugin that uses this well is Soliloquy (a really robust slider plugin). Here’s a sample modeled on this plugin’s code:

Thomas Griffin, the developer behind Soliloquy, presents three reasons for using the Settings API to output admin notices and errors:

  1. You ensure that your notices will remain consistent for any WordPress version, and
  2. You can register all your messages through one slug and output them with a simple  settings_errors() call
  3. You can use add_settings_error() anywhere before the admin_notices hook to register your messages, and since they are all registered through one slug, it makes it really easy to create new notices.

This StackExchange thread contains an excellent explanation, while Konstantin Kovshenin also makes reference to the display of notices and errors using the Settings API in his talk at WordCamp Bulgaria.

Here’s yet another code sample on how to use notices and errors in a simple way.

Codex reference:

http://codex.wordpress.org/Function_Reference/add_settings_error

http://codex.wordpress.org/Function_Reference/settings_errors

If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.

About Jean Galea

Jean Galea is a WordPress developer, entrepreneur and padel player. He is the founder of WP Mayor, the plugins WP RSS Aggregator and EDD Bookings, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Related Articles

Leave a Reply