Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In my admin panel, if I use a sanitizing callback register_setting() and the field does not pass through my custom validation, I've been preventing the database from updating by using return FALSE; and adding an error with add_settings_error().

How do I return the invalid values back to the form fields that the user submitted, so that they don't have to start all over again?

share|improve this question
add comment

1 Answer

This a really good question - my suggestion is to use transients. For instance, in your validation callback:

 wpse51669_validation_cb($settings){
     //Perform validation checks

     if( $valid ){
        //If settings validate
        return $validate_settings;
     }

     //Otherwise add settings error
     add_settings_error('my-plug-in-settings','error-with-xyz', 'I fell over','error');

     //And add the failed settings to a transient
     set_transient( 'my-plug-in-settings-invalid', $settings, 60);

     return false;
 }

Then on your settings page, if the options have been validated - check for the transient, and use the 'failed' settings, if they are found:

    if( isset($_GET['settings-updated']) && $_GET['settings-updated'] && get_transient('my-plug-in-settings-invalid') ){
        $options_to_display  = get_transient('my-plug-in-settings-invalid');
        delete_transient('my-plug-in-settings-invalid');    

    }else{
        $options_to_display = get_option('my-plug-in-settings');
    }
share|improve this answer
    
is there any way to do this without using the db? –  Dave Kiss May 13 '12 at 4:18
    
@DaveKiss - I don't think so, but is there any reason you don't want to use the db? –  Stephen Harris May 13 '12 at 18:02
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.