-
-
Save tommcfarlin/d197ba00c60d61efed3e to your computer and use it in GitHub Desktop.
[WordPress] Sanitizing Arrays: The WordPress Settings API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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