Obfuscate email addresses with a shortcode

**UPDATE** I’ve released a slightly modified version of this into a plugin that has a TinyMCE button. You can get it here.

Folks that have been on the web for any length of time know that putting your email address on a forward-facing website is just asking for spam. While Gmail and other providers are pretty good at flagging them, it’s still an unnecessary burden, especially if you are using your own mailserver. But for many businesses and sites, you’ve gotta have it out there. So what to do?

The folks over at catswhocode recently published a post with a handful of WP shortcodes, one of which was obfuscating an email address using a convoluted regex and count method. Well, you could do that. But why not use a built-in WP function?

WordPress has an antispambot function that’s been in core since version 0.71. The only problem is that you can’t use it in a post, page, or widget on it’s own. So I’ve written a simple shortcode to allow you to call that function wherever you may need it.

This would go into your functions.php file

function emailbot_ssc($attr) {
		extract( shortcode_atts( array(
			'address'	=> '',
		), $attr ) );
	return '<a class="email_link" href="mailto:'.antispambot($attr['address']).'" title="Send Us An Email" target="_blank">'.antispambot($attr['address']).'</a>';
	}
add_shortcode("email", "emailbot_ssc");

And to use the shortcode (on the HTML tab, not the visual editor)

[email address="you@emailcompany.com"]

that’s it. And remember, if you need to use this (or any shortcode) in a widget, you’ll need to enable it if your theme hasn’t already done so.

{ 8 comments… read them below or add one }

Willie Jackson November 28, 2011 at 11:00 am

Very useful mate, nice work.

Reply

Dave Wilkinson November 28, 2011 at 11:45 am

Awesome stuff. Thanks for showing me the error of my ways :)

Reply

Norcross November 28, 2011 at 11:52 am

Just happy to pass it all along.

Reply

Phillip November 28, 2011 at 12:07 pm

I’m not sure I agree with the premise that some businesses *need* to have their email publicly displayed on their site, but regardless this is a good snippet :)

Reply

Norcross November 28, 2011 at 12:14 pm

Agreed. There are studies about how some folks won’t fill out a contact form, but will send an email if the address is there. But I’m not one of those studies people. I just build shit :)

Reply

Dion Evans December 1, 2011 at 9:38 am

That is a great tool! I have been directing clients away from using e-mail addresses to contact form because of the potential for spam. Thanks for the work you did on this.

Reply

Norcross December 1, 2011 at 9:40 am

I’m a big believer in having a contact form, since usually there is a need to collect specific information. But there’s definitely a place for using the email address straight-up.

Reply

Dino December 23, 2011 at 4:56 am

Thanks for sharing. Super simple and works a treat. A virtual beer coming your way…

Reply

Leave a Comment

Previous post:

Next post: