2

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

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

Then my validation:

  $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.

1
  • Exactly what kind of errors are you hoping to show? Unless your setting error_found somewhere ( missing $? ) then Wordpress will simply return a WP_Error object or null on a call, or it'll output to the browser that the user has arrived somewhere they shouldn't Commented Nov 18, 2011 at 10:59

2 Answers 2

2

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
  • Thanks, Tom. error_found is pseudo code standing in for a real error. I'm also wondering how I can cancel the normal save settings process if I find errors? Commented Nov 20, 2011 at 22:19
  • I don't know if you can other than possibly a wp_redirect followed by a die(), but that sounds terribly hackish Commented Nov 22, 2011 at 11:04
0

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.

3
  • 1
    Please add the solution as an answer. A good answer should not rely on an external link. Commented Aug 29, 2013 at 14:59
  • I realized that but it's a huge chunk of text spanning multiple pages. Commented Aug 29, 2013 at 15:53
  • @Borgboy And the link doesn't even work now. Commented Jan 3, 2024 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.