I had an array of email addresses ($email_addresses
) in WordPress (PHP).
The best practice is, of course, to sanitize all addresses. This can be done easily using the built-in sanitization function in WordPress: sanitize_email
.
The old method would be to use foreach
and sanitize each email address separately. For example:
$sanitized_email_addresses = array();
foreach ( $email_addresses as $email_address ) {
$sanitized_email_addresses[] = sanitize_email( $email_address );
}
However, this can be done with only one line using array_map
:
// Sanitize all email addresses.
$email_addresses = array_map( 'sanitize_email', $email_addresses );
This will run the sanitize_email
function for each of the values in the array.
We could, of course, use any other sanitization function as needed (for instance, sanitize_text_field
for an array of text inputs).