Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created June 3, 2015 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/3ecc9e078352ecb5da61 to your computer and use it in GitHub Desktop.
Save tommcfarlin/3ecc9e078352ecb5da61 to your computer and use it in GitHub Desktop.
[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