Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Sanitization Classes of the WordPress Settings API
<?php
class Address_Setting {
/**
* A custom sanitization function that will take the incoming input, and sanitize
* the input before handing it back to WordPress to save to the database.
* If the input is not valid, then the data will not be saved and an error
* message will be added to WordPress' error collection.
*
* @since 1.0.0
*
* @param array $input The address input.
* @return mixed $new_input The sanitized input or false (to prevent the data from saving)
*/
public function sanitize( $input ) {
$new_input = false;
$validator = new Address_Validator( $this );
if ( $validator->is_valid( $input ) ) {
$new_input = array();
foreach ( $input as $key => $val ) {
$new_input[ $key ] = sanitize_text_field( $val );
}
}
return $new_input;
}
}
<?php
class City_Setting {
/**
* A custom sanitization function that will take the incoming input, and sanitize
* the input before handing it back to WordPress to save to the database.
* If the input is not valid, then the data will not be saved and an error
* message will be added to WordPress' error collection.
*
* @since 1.0.0
*
* @param array $input The city input.
* @return mixed $new_input The sanitized input or false (to prevent the data from saving)
*/
public function sanitize( $input ) {
$new_input = false;
$validator = new Postal_Code_Validator( $this );
if ( $validator->is_valid( $input ) ) {
$new_input = array();
foreach ( $input as $key => $val ) {
$new_input[ $key ] = sanitize_text_field( $val );
}
}
return $new_input;
}
}
<?php
class Postal_Code_Setting {
/**
* A custom sanitization function that will take the incoming input, and sanitize
* the input before handing it back to WordPress to save to the database.
* If the input is not valid, then the data will not be saved and an error
* message will be added to WordPress' error collection.
*
* @since 1.0.0
*
* @param array $input The city input.
* @return mixed $new_input The sanitized input or false (to prevent the data from saving)
*/
public function sanitize( $input ) {
$new_input = false;
$validator = new Postal_Code_Validator( $this );
if ( $validator->is_valid( $input ) ) {
$new_input = array();
foreach ( $input as $key => $val ) {
$new_input[ $key ] = sanitize_text_field( $val );
}
}
return $new_input;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.