Ask your WordPress questions! Pay money and get answers fast! (more info)

Automatically remove spaces from email field

  • SOLVED

I have a client that is being very particular: they don't want to email field to be 'required', but they would also like emails that are entered accidentally with spaces to still work. Is it possible to have php remove any spaces that are entered in the email field in question? It's the billing_email field in woocommerce that I am working on.

Answers (2)

2016-01-23

Romel Apuya answers:


add_filter( 'woocommerce_checkout_fields' , 'remove_spaces_in_email' );
function remove_spaces_in_email( $fields ) {
$fields['billing']['billing_email']= str_replace(' ','',$fields['billing']['billing_email']);
return $fields;
}


michaelmiller comments:

I think both your options are correct, but Romels code was the one that didn't cause a php error on that particular site (quite a few plugins) - so thank you! Very helpful.

2016-01-23

Reigel Gallarde answers:

email is required in wordpress to create a user. Email is an important part of user management. How are you doing it when creating users without email?

however, if adding emails with spaces, this should remove those white spaces.

add_filter( 'woocommerce_checkout_fields' , 'strip_white_spaces' );

function strip_white_spaces( $fields ) {

$fields['billing']['billing_email'] = preg_replace('/\s+/', '', $fields['billing']['billing_email']);

return $fields;

}