Skip to content

Instantly share code, notes, and snippets.

@sumitpore
Created April 20, 2020 09:28
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 sumitpore/64d3d659f40aa913861bf93448c1a823 to your computer and use it in GitHub Desktop.
Save sumitpore/64d3d659f40aa913861bf93448c1a823 to your computer and use it in GitHub Desktop.
Array Escape in WordPress. To be used with 'IN' keyword
/**
* Format Array to make useful in SQL Queries
*
* @param array $array Array to be escaped.
* @return string
*/
function escape_array( $array = array() ) {
global $wpdb;
$escaped = array();
foreach ( $array as $k => $v ) {
if ( is_numeric( $v ) ) {
$escaped[] = $wpdb->prepare( '%d', $v );
} else {
$escaped[] = $wpdb->prepare( '%s', $v );
}
}
return implode( ',', $escaped );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment