Persist fields with Setting API

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?

Related posts

Leave a Reply

1 comment

  1. 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');
        }