Add Snippet To Project
Hyperlinking an email address in WordPress may seem like a simple task, but for those new to the platform, it can be a bit confusing. In this article, we’ll guide you through the process of adding a hyperlink to an email address in WordPress using both the visual editor and the text editor. Whether you’re a blogger, business owner, or web developer, being able to hyperlink email addresses is a valuable skill that can help you improve the user experience of your site. Let’s dive in!
function wpturbo_hyperlink_email_address( $email ) {
return '<a href="mailto:' . antispambot( $email ) . '">' . antispambot( $email ) . '</a>';
}
The code snippet above creates a function called wpturbo_hyperlink_email_address()
, which hyperlinks an email address in WordPress. The function takes an email address as its parameter, which is then processed using the WordPress antispambot()
function.
The first thing that the function does is it creates an <a>
tag with the appropriate href
attribute to create a hyperlink. The href
attribute is set to mailto:
followed by the processed email address using the antispambot()
function. This function helps to obfuscate the email address to prevent it from being targeted by spammers.
return '<a href="mailto:' . antispambot( $email ) . '">' . antispambot( $email ) . '</a>';
Notice that the processed email address is used twice within the <a>
tag. This is because the actual email address is obfuscated using the antispambot()
function, so we need to use the processed email address twice to get the desired result.
Lastly, we return the hyperlink string. This function can be used in WordPress templates or plugins to automatically hyperlink any email addresses displayed on a webpage.