How to obfuscate email using shortcode
<?php
/**
* Shortcode for the email obfuscation
* It will be used so we can have an email address displayed on the website
* Accepts:
* address - email address - required
* name - what to display on the front end. If left blank, email address will be used
* Usage: [ email address="[email protected]" name="Office" ]
*
**/
function zd_email_shortcode( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'address' => '',
'name' => '',
),
$atts,
'email'
);
$sanitizedEmail = sanitize_email( $atts['address'] );
$sanitizedName = sanitize_text_field( $atts['name'] );
if ( $sanitizedEmail )
{
$antispamEmail = antispambot( $sanitizedEmail );
return '<a href="mailto:' . $antispamEmail . '">' . ( $sanitizedName != '' ? $sanitizedName : $antispamEmail ) . '</a>';
}
}
add_shortcode( 'email', 'zd_email_shortcode' );
?>