• 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!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Thomas Griffin

    (@griffinjt)

    This issue is killing me! I’d love help ??

    Thread Starter Thomas Griffin

    (@griffinjt)

    Anyone out there with any tips?

    Unless I’m reading it wrong, you are telling it to use either what is in $input, if $input isn’t empty, or use what is in the database. You will never be able to revert back to nothing given that logic. Once you put something in you are always going to have something.

    You will need to rethink the logic. For one, if you want your user to able to clear the field, why are you switching on not empty (I’d use !empty() instead of '' != if it were me.)? It looks to me like you should just accept the user input. That way, if the user clears the field in the form it would be cleared in the db too. Am I missing something?

    Thread Starter Thomas Griffin

    (@griffinjt)

    Ahh, genius! I’ve just been staring at the code too long. That’s all I needed was a little spark. Updated it to show this:

    // Scripts section
    		$validated_input['inline_header_scripts'] = ( ! empty( $input['inline_header_scripts'] ) ? trim( $input['inline_header_scripts'] ) : '' );
    		$validated_input['inline_footer_scripts'] = ( ! empty( $input['inline_footer_scripts'] ) ? trim( $input['inline_footer_scripts'] ) : '' );

    I don’t know why I didn’t continue the !empty() logic (I even had it in the lines before..)..then I just added the statement that if it isn’t empty, output what is there, and if it is empty, output nothing. It works for me, but if you have a cleaner way to do it, feel free to share it with me.

    Thread Starter Thomas Griffin

    (@griffinjt)

    The reason why I was using !empty() is because in some random occasion it may be that a text field or textarea has a value by default. It isn’t like to happen, but who knows.

    Thread Starter Thomas Griffin

    (@griffinjt)

    Ok, I get where you are going now. Updated one more time – I think my logic is correct now and the data should be sanitized correctly:

    // Scripts section
    		$validated_input['inline_header_scripts'] = esc_textarea( trim( $input['inline_header_scripts'] ) );
    		$validated_input['inline_footer_scripts'] = esc_textarea( trim( $input['inline_footer_scripts'] ) );
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Issues with Validating Input with Settings API’ is closed to new replies.