Stop spam in WordPress: How to (Updated for 2020)

This is a quick and super clean trick to stop spambots from scrapping all the emails off your website.

Stop Wordpress Spam

Photo by Pau Casals on Unsplash

It converts selected email addresses characters to HTML entities to block spambots. Not all characters in the email address are converted: the selection is random and changes each time the function is called.

To use this in your WordPress Content area all you have to do it wrap the email address in a shortcode:

[email]john.doe@mysite.com[/email]

Insert this code into your child theme’s functions.php file to make it work:

/**
 * Hide email from Spam Bots using a shortcode.
 * @param array  $atts    Shortcode attributes. Not used.
 * @param string $content The shortcode content. Should be an email address.
 * @return string The obfuscated email address. 
 */
function wpcodex_hide_email_shortcode( $atts , $content = null ) {
	if ( ! is_email( $content ) ) {
		return;
	}
	$content = antispambot( $content );
	$email_link = sprintf( 'mailto:%s', $content );
	return sprintf( '<a href="%s">%s</a>', esc_url( $email_link, array( 'mailto' ) ), esc_html( $content ) );
}
add_shortcode( 'email', 'wpcodex_hide_email_shortcode' );

Reference: https://codex.wordpress.org/Function_Reference/antispambot

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *