• In my plugin, I add a an option to the options page as follows:

    register_setting('sbs_gig_options','sbs_gig_options','sbs_gig_validate_options');
    add_settings_section('sbs_gig_main',__('Settings','sbs_gig'),'sbs_gig_lastfm_text','sbs_gig');
    add_settings_field('sbs_gig_lastfm_key',__('Last FM Key','sbs_gig'),'sbs_gig_lastfm_key','sbs_gig','sbs_gig_main');

    and the validation function looks like this:

    function sbs_gig_validate_options( $input ) {
    // Create our array for storing the validated options
        $valid = array();
        // Loop through each of the incoming options
        foreach( $input as $key => $value ) {
            // Check to see if the current option has a value. If so, process it.
            if( isset( $input[$key] ) ) {
                // Strip all HTML and PHP tags and properly handle quoted strings
                $valid[$key] = strip_tags( stripslashes( $input[ $key ] ) );
                if ($key=='lastfmkey') {
                    if (!sbs_gig_verify_lastfm($valid[$key])) {
                        add_settings_error('sbs_gig_lastfm_key','lastfmkey_error',__('Please enter a valid Last FM API Key','error'));
                        $valid['lastfmkey']='';
                    }
                    else {
                        $valid['lastfmkey']=$input[$key];
                    }
                }
            } // end if
        } // end foreach
        // Return the array processing any additional functions filtered by this action
        return $valid;
    }

    From what I’ve read, the error I add via add_settings_error() should be displayed automatically, but it’s not. I have to explicitly call settings_error() to get the error to display. Can anyone suggest why?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Spiralli

    (@spiralli)

    Correction:
    add_settings_error('sbs_gig_lastfm_key','lastfmkey_error',__('Please enter a valid Last FM API Key','sbs_gig'),error');

    That is correct behavior. The settings errors will only be displayed if you add settings_errors() to your options page.

    Thread Starter Spiralli

    (@spiralli)

    Thanks. None of the books or documentation I’ve read mention this.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add_settings_error() not working unless I call settings_errors()’ is closed to new replies.