WordPress.org

Ready to get started?Download WordPress

Forums

add_settings_error() not working unless I call settings_errors() (4 posts)

  1. Spiralli
    Member
    Posted 1 year ago #

    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?

  2. Spiralli
    Member
    Posted 1 year ago #

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

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

  4. Spiralli
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic