In this tutorial we're going to learn how to create a WordPress shortcode that allows you to link to an email address from your content, to do this we're going to use the WordPress shortcode generator.
Linking To An Email Address
If you want to make it easy for your visitors to email you from your website you can create a link to your email with the the value mailto:.
<a href="mailto:mail@example.com">Email me</a>
This link will open up your default mailing client and start an email to mail@example.com try it from the link below.
Along with the mailto: link you can default attributes to the generated email by adding parameters for:
- Subject
- CC or BCC
- Body text
Add A Subject
To add a subject to the link you can provide subject query string to the end of the link.
<a href="mailto:mail@example.com?subject=This is a new subject" class="btn btn-flat">Email me</a>
Add CC Or BCC
To add a CC or a BCC to the default email you can add these to the link query string.
<a href="mailto:mail@example.com?subject=This is a new subject&cc=anotheremail@example.com" class="btn btn-flat">Email me</a>
Add Default Message
To add a default message to the link you can use a body querystring.
<a href="mailto:mail@example.com?subject=This is a new subject&cc=anotheremail@example.com" class="btn btn-flat">Email me</a>
Spambot Protection
The problem having your email on a page like this is that spambots will scan your website for emails in your HTML and collect them to add to an email list. Therefore you need to protect your email from spambots by obfuscating your email on the page. This makes sure that spambots scanning your website can not pick up your email address to add to their mailing list.
In WordPress you can easily do this with a built in function antispambot, which will convert an email into HTML entities.
<?php antispambot( $emailaddy, $hex_encoding ) ?>
This is the function we need to use in our WordPress shortcode when outputting a link on the page.
WordPress Shortcode Generator
The WordPress shortcode generator allows us to easily generate the code to create shortcodes for our WordPress site.
On the first tab we setup the shortcode name and function name we need to use. This shortcode is going to be called antispambot-email and will take one attribute that will be the email we want to display on the page.
On the attribute tab add one attribute called email and set a default value of example@email.com.
On the code tab we need to use the antispambot function whenever we want to output the email address.
return '<a href="mailto:' . antispambot( $email ) . '">' . antispambot( $email ) . '</a>';
Next you can click the Generate code button and the tool will create all the code you need to add this shortcode to your WordPress site.
function wp_antispambot_email_function ( $atts )
{
// Attributes
extract( shortcode_atts(
array(
'email' => 'example@email.com'
), $atts ));
// Code
return '<a href="mailto:' . antispambot( $email ) . '">' . antispambot( $email ) . '</a>';
}
add_shortcode( 'antispambot-email', 'wp_antispambot_email_function' );
Try out the WordPress shortcode generator to create your own new shortcodes.