Settings API – sanitizing urls, email addresses and text

Instead of using add_settings_section() and add_settings_field() every time, create a function that returns an array of options for example:

function my_theme_options() {
$options = array();

$options[] = array(
                'id' => 'ID',
                'title' => 'Title',
                'type' => 'text_field', // use this value to sanitize/validate input
                'validate' => 'url' // use this value to validate the text as url
                // add as much as you need like description, default value ...
            );

$options[] = array(
                'id' => 'ID_2',
                'title' => 'Title',
                'type' => 'text_field',
                'validate' => 'email' // use this value to validate the text as email
                // add as much as you need like description, default value ...
            );

// every time you want to add a field you'll use this function an create a new array key $options[] = array();

return $options;

}

using this function we can register each field with a foreach loop that will use add_settings_field()

now using this function you can create one callback function for register_setting() and use switch to validate the input for example:

// this should be the callback function of register_setting() (last argument)
function validate_settings($input) {
$options = my_theme_options(); // we'll set $options variable equal to the array we created in the function before

$valid_input = array(); // this will be the array of the validated settings that will be saved to the db, of course using one array for all options.

foreach ($options as $option) {
    switch ( $option['type'] ) { // $option['type'] where type is the key we set before in my_theme_options()
        case 'text_field':
            // inside we'll create another switch that will use the validate key we created in my_theme_options()
            switch( $option['validate'] ) {
                case 'url':
                    // validate url code

                break;

                case 'email':
                    // validate email
                break;

                // create a default for regular text fields
                default:
                    // default validation
                break;
            }
        break;

        case 'textarea':
            // your validation code here
        break;

        // you get the idea just keep creating cases as much as you need
    }// end switch
}// end foreach

return $valid_input;
}

at the end of each case to save the value to $valid_input array

$valid_input[$option['id']] = $input[$option['id']]

for example for validating the url use:

if ( preg_match('your regex', $input[$option['id']]) ) {
    $valid_input[$option['id']] = $input[$option['id']];
}

you can also create a function just like the options function but for sections and create a foreach loop that will use add_settings_section(), you get the idea this will be much easier for you, you’ll save a lot of time later when you want to add new settings fields and section.
hope that helps 🙂

Leave a Comment