Obfuscate email addresses with a shortcode

Norcross Nov 28th, 2011shortcodes

**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

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.

15 Responses to “Obfuscate email addresses with a shortcode”

  1. Phillip

    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 🙂

    • Norcross

      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 🙂

  2. Dion Evans

    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.

    • Norcross

      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.

  3. Dino

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

  4. Chris

    Great, simple tool… love it. I was wondering if there is a way to set it so it pre-fills the subject line?

      • Chris

        Thanks for the reply. That was what I was afraid of… I think I would rather have the obfuscating than the pre-fill.