Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the Settings API to save some of my plugin options. I also have a notice in the admin related to the plugin, which basically shows once upon activation, and then can be dismissed at the click of a button (working via AJAX). I need to store a value in the DB for the notice, so that if the value is 1 the notice won't be shown, and shown if the value is false.

I decided to store the value of the notice within the same field as the rest of this plugin's settings, however I started getting an error that the index was undefined exactly after saving the settings. Of course this was due to the validation routine. So my question is whether it is good practice to just set the output value to what is already stored in the DB within the validation routine, so nothing will go wrong. The alternative is to store the notice option independently in another field, but I'm not sure that's necessary.

So in essence this is what I have at the moment within my validation function:

 /** 
 * Validate inputs from the general settings page
 */
function myplugin_settings_general_validate( $input ) {
    $options = get_option( 'myplugin_settings_general' );  

    // Create our array for storing the validated options
    $output = 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
            $output[ $key ] = strip_tags( stripslashes( $input[ $key ] ) );

        } // end if

    } // end foreach

    // TO AVOID ERRORS WITH UNDEFINED INDEX 
    $output['hide_notification'] = $options['hide_notification'];

    // Return the array processing any additional functions filtered by this action
    return apply_filters( 'myplugin_settings_general_validate', $output, $input );
}
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.