Displaying errors with settings api

I’m not using the full settings api, just this code:

register_setting( 'my_options', 'my_options', 'my_options_validate' );

Then my validation:

Read More
  $options = get_option('my_options');

  if(error_found){

    add_settings_error( 'my_options', 'settings_updated', 'error_message_here', 'error');

  }

  return $options;

But on postback no errors are displayed. So my questions are:

does simply setting an error message result in its display?
do I have to use the full settings api to have errors displayed?
do I have do anything to disable the “settings updated” message on postback?

I’ve tried using settings_errors but no errors show.

Related posts

Leave a Reply

2 comments

  1. does simply setting an error message result in its display? do I have to use the full settings api to have errors displayed? do I have do anything to disable the “settings updated” message on postback?

    You need to add a call to settings_errors, e.g.:

    /**
     * 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' );
    

    If you don’t, then the errors you added will not be displayed. You may also want to check that $error_found is true. It is not a WP constant I recognise, so I assume it is part of your code ( and hasn’t been set ) ( also, it’s missing a $ at the start? ).

    There is also the possibility that no errors have occurred, or that your validation is allowing invalid cases to pass through.

  2. I had a similar issue when I was developing my ArticleForge WordPress plugin. I did not want to allow erroneous input to enter the database, but yet I wanted to give the user a chance to correct their input. So, after much creative process, I did devise a method that works. You don’t have to do a wp_redirect or a die(). The main gist of it is that you switch out the users input during validation with that of the existing options and return those values instead.

    You can check out my blog post for a detailed description of the process I followed.