X

How To Create a Shortcode That Obscures Email Links

Snippets by IsItWP

Are you looking for a way to create a shortcode that obscures email links? This snippet will let you easily and quickly obscure an email address link to make it harder for spam bots to find your email address.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

<?php

// Obfusticate email addresses
function obf_mail_shortcode( $atts , $content=null ) {
    for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';';
    return '<a href="mailto:'.$encodedmail.'">'.$encodedmail.'</a>';
}
add_shortcode('mailto', 'obf_mail_shortcode');

?>

You can wrap this shortcode around an email address to obscure the email address.


[mailto][email protected][/mailto]

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 24 best WordPress education themes and 8 best WordPress review plugins.

Comments   Leave a Reply

  1. Johnathan Christopher February 11, 2019 at 6:00 pm

    Thanks for the snippet but it returns an error:
    Notice: Undefined variable: encodedmail in …/functions.php on line 274

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!