› News › Web Development
 › Stop spam in WordPress: How to (Updated for 2020)
   -   3 March, 2020

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

Photo by Pau Casals on Unsplash

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

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

CATEGORIES
POPULAR ARTICLES
The importance of consistency to build a solid brand.
The importance of consistency to build a solid brand.

Branding is much more than just a cool logo or a well-placed advertisement. A solid brand is the foundation upon which your corporate culture and future growth is built. Crucial to every business, it tells people that you care about what you deliver, that you live up...

Create buttons with rounded corners for emails – How to
Create buttons with rounded corners for emails – How to

The other week I had a client who was insistent on having their rounded buttons in their new HTML Mailchimp template. After spending way too long tearing my hair out trying to get it working in all clients, I stumbled across the below hack. The below code is a simple...

Backing up your WordPress Website – How to
Backing up your WordPress Website – How to

There are many reasons to backup your WordPress website before making major changes or updating core functionalities. Backing up is one of the best things you can do to give you peace of mind when the catastrophic happens. Your website is worth all the money and time...

TAGGED WITH

//

We want to hear what you think about this article, or what you would like us to write about next! Submit a comment below or write to us at hello@studiotwofold.com.au.
Studio Twofold provides creative design services and advice to growing Australian businesses to help them feel confident and kick goals. Learn more about us.

READ THESE NEXT

0 Comments

Submit a Comment

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

Want to talk?

Our process begins with a conversation about your business and how we can help.