Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] Sanitizing Arrays: The WordPress Settings API
<?php
/**
* 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.
*
* @since 1.0.0
*
* @param array $input The address input.
* @return array $new_input The sanitized input.
*/
public function sanitize( $input ) {
// Initialize the new array that will hold the sanitize values
$new_input = array();
// Loop through the input and sanitize each of the values
foreach ( $input as $key => $val ) {
switch ( $key ) {
case 'address_1':
$new_input[ $key ] = sanitize_text_field( $val );
break;
case 'address_2':
$new_input[ $key ] = sanitize_text_field( $val );
break;
case 'city':
$new_input[ $key ] = sanitize_text_field( $val );
break;
case 'province':
$new_input[ $key ] = sanitize_text_field( $val );
break;
case 'postal_code':
$new_input[ $key ] = sanitize_text_field( $val );
break;
}
}
return $new_input;
}
<?php
/**
* 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.
*
* @since 1.0.0
*
* @param array $input The address input.
* @return array $new_input The sanitized input.
*/
public function sanitize( $input ) {
// Initialize the new array that will hold the sanitize values
$new_input = array();
// Loop through the input and sanitize each of the values
foreach ( $input as $key => $val ) {
$new_input[ $key ] = sanitize_text_field( $val );
}
return $new_input;
}
<?php
/**
* 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.
*
* @since 1.0.0
*
* @param array $input The address input.
* @return array $new_input The sanitized input.
*/
public function sanitize( $input ) {
// Initialize the new array that will hold the sanitize values
$new_input = array();
// Loop through the input and sanitize each of the values
foreach ( $input as $key => $val ) {
$new_input[ $key ] = ( isset( $input[ $key ] ) ) ?
sanitize_text_field( $val ) :
'';
}
return $new_input;
}
<?php
/**
* 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.
*
* @since 1.0.0
*
* @param array $input The address input.
* @return array $new_input The sanitized input.
*/
public function sanitize( $input ) {
// Initialize the new array that will hold the sanitize values
$new_input = array();
// Loop through the input and sanitize each of the values
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.