Issues with Validating Input with Settings API
-
I’m having some issues validating input with the Settings API. Specifically, I’m having an issue with clearing a textarea and it reverting to the previously entered data. Here is the code I am using for validation:
function inline_validate_options( $input ) { $inline_options = get_option( 'inline_theme_options' ); $validated_input = $inline_options; $submit_options = ( ! empty( $input['submit-inline-options'] ) ? true : false ); // This sets up our save option. $reset_options = ( ! empty( $input['reset-inline-options'] ) ? true : false ); // This sets up our reset option. if ( $submit_options ) { // Scripts section $validated_input['inline_header_scripts'] = ( '' != $input['inline_header_scripts'] ? trim( $input['inline_header_scripts'] ) : $validated_input['inline_header_scripts'] ); $validated_input['inline_footer_scripts'] = ( '' != $input['inline_footer_scripts'] ? trim( $input['inline_footer_scripts'] ) : $validated_input['inline_footer_scripts'] ); } elseif ( $reset_options ) { $inline_default_options = inline_default_theme_options(); // Scripts section $validated_input['inline_header_scripts'] = $inline_default_options['inline_header_scripts']; $validated_input['inline_footer_scripts'] = $inline_default_options['inline_footer_scripts']; } return $validated_input; }
As you can see, I have set it to where if the textarea is not empty ( ” != ), it will be filled with whatever is in the textarea, but if it is, it reverts back to the default option, which in this case is nothing. However, this is only functional if no option has previously been stored by the user. If something has already been entered, even when the user tries to clear the field, it will revert back to the stuff previously entered.
How can I do a check to see if the current textarea field is empty, and if so, update that in the database.
Also, on a side note, how can I remove anything not within HTML tags? I know I can setup a filter with wp_kses to only allow certain things, but even at that, I’m not familiar enough with the add_settings_error part of the API to know how to use it in the validate_options callback.
Any help and enlightenment would be appreciated!
- The topic ‘Issues with Validating Input with Settings API’ is closed to new replies.