How to validate my Wordpress plug-in settings?

Viewed 36

SO I have my plug-in at 90% completed but it is only sanitizing / escaping the inputs. How do I conditionally validate it before the settings are saved?

Quick example:

<form method="post" action="options.php">
    <?php
        settings_fields( 'settings_option_group' );
        do_settings_sections( 'settings-admin' );
        submit_button();
    ?>
</form>

This is the settings page form, it handles everything related to show / save the settings. The functions such as this one:

if ( isset( $input['stwc_ft_oosn_notice_1'] ) ) {
    $sanitary_values['stwc_ft_oosn_notice_1'] = sanitize_text_field( $input['stwc_ft_oosn_notice_1'] );
}

Sanitize the user input stripping what is not required, but how do I check for example, that a text box has a number? Or how do I check if a textbox is not null conditionally? For example:

if xxx checkbox is checked then yyy textbox must have a value

I could not find this case in the whole codex, following the tutorial relative to writing a plugin it indeed mention the Data Validation and the ways you can achieve it. But in practice it gives no indication about how to do it in the settings page, for example I'd show an error message when the settigns cannot be saved due to logic, or add a red border or a wrong / empty field. etc..

0 Answers
Related