Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Help with Validating Input with Settings API WordPress

  • SOLVED

I have 2 questions.

1) I'm using the Settings API for theme options, and I have an issue with text fields/textareas. I know my code isn't quite right, but I'm not exactly sure what to use.

Here is my validate input function:

function random_validate_options( $input ) {

$random_options = get_option( 'random_theme_options' );
$validated_input = $random_options;

$submit_options = ( ! empty( $input['submit-random-options'] ) ? true : false ); // This sets up our save option.
$reset_options = ( ! empty( $input['reset-random-options'] ) ? true : false ); // This sets up our reset option.

if ( $submit_options ) {

// Scripts section
$validated_input['random_header_scripts'] = ( '' != $input['random_header_scripts'] ? trim( $input['random_header_scripts'] ) : $validated_input['random_header_scripts'] );
$validated_input['random_footer_scripts'] = ( '' != $input['random_footer_scripts'] ? trim( $input['random_footer_scripts'] ) : $validated_input['random_footer_scripts'] );

}

elseif ( $reset_options ) {

$random_default_options = random_default_theme_options();

// Scripts section
$validated_input['random_header_scripts'] = $random_default_options['random_header_scripts'];
$validated_input['random_footer_scripts'] = $random_default_options['random_footer_scripts'];

}

return $validated_input;

}


It has a submitted and reset option. On the submitted option, if there is nothing in the db, then it defaults to the default option I've set. However, what if someone has already entered something, saved it, and then decides to remove it? Even if they clear the field, upon save it will still revert back to whatever is in the db. What code do I need to use to check if the current field is empty, and if so, update that accordingly upon save?

2) Might be simple, might not be. I'm learning. :) What code can I use to remove any text that is not within an html tag? I know I can use wp_kses, but I don't know how to use the add_settings_error option with the Settings API to let someone know if wp_kses was activated or not.

Any help is appreciated!

Answers (2)

2011-06-17

Fahad Murtaza answers:

OK, here is the thing

For your questions # 1. You need to post complete code of how you are using Settings API as there might be issue with other functions you are using in the code above. What we need necessarly is how to replicate the same thing on our machine so we can be of help.

For # 2, please explain what you mean by, '' <blockquote>any text that is not within an html tag?</blockquote>

2011-06-17

Utkarsh Kukreti answers:

I'm not sure about add_settings_error, but you could do something like this to solve both the problems. You may have to tweak the wp_kses parameters ([[LINK href="http://codex.wordpress.org/Function_Reference/wp_kses"]]ref[[/LINK]]).

if ( $submit_options ) {

// Scripts section

$validated_input['random_header_scripts'] = wp_kses($input['random_header_scripts']);

$validated_input['random_footer_scripts'] = wp_kses($input['random_footer_scripts']);

}