Spam-Protected Email Shortcode
Adds an [email] shortcode that prints a clickable email address encoded so simple spam bots cannot harvest it.
Opens WPIDE on
Code
add_shortcode( 'email', function ( $atts, $content = '' ) {
$atts = shortcode_atts( array( 'address' => '', 'label' => '' ), $atts, 'email' );
$address = sanitize_email( '' !== $atts['address'] ? $atts['address'] : wp_strip_all_tags( (string) $content ) );
if ( ! is_email( $address ) ) {
return '';
}
$label = '' !== $atts['label'] ? esc_html( $atts['label'] ) : antispambot( $address );
return '<a href="mailto:' . antispambot( $address, 1 ) . '">' . $label . '</a>';
} );
Notes
Example: [email][email protected][/email] or [email address="[email protected]" label="Email us"].
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.