Paulund
2012-05-04 #wordpress

Display Email Addresses In Wordpress

There are loads of spam bots crawling the web looking for people who display their email address on web page without encoding it. Spam bots will crawl your pages exactly the same way Search Engines crawls your pages, but while Search Engines are crawling to index your content, spam bots are crawling to find certain information. One of the most common bits of data they are searching for are email addresses, this is because they can store these email addresses in a list and send out spam emails. Displaying your email address on your web page makes it very easy for your visitors to store this and send you email, but you will need to get around the spam bot problem to do this you will need to HTML encode your email address. Luckily for Wordpress users there is a inbuilt function that will do this for you antispambot(). All you to do is add the following function in your code where ever you want to display your email address.

antispambot( $mail );

Adding Email Address In Post

Now we know we can encode the email address we might want to add this to a post, but in some blogs you can't run PHP code in a post unless you have plugins to help. In order to run PHP code in posts you will need to create a shortcode that you can call to add an email address in your posts. First we need to create the shortcode, open your functions.php file and add the following code.


function email_encode( $atts, $email ){
    return '<a href="mailto:'.antispambot($email).'">'.antispambot($email).'</a>';
}
add_shortcode( 'email', 'email_encode' );

In your posts you can do use this shortcode to create a link with your email address.


[email]your at emailaddress dot com[/email]