antispambot() wordpress function and usage This text is not used because the data attribute has a value

antispambot() wordpress function and usage

Home / WordPress / WordPress Wiki / antispambot() wordpress function and usage

antispambot( string $email_address, int $hex_encoding )
Converts email addresses characters to HTML entities to block spam bots.

Parameters

$email_address

(string) (Required) Email address.

$hex_encoding

(int) (Optional) Set to 1 to enable hex encoding.

Return

(string) Converted email address.

Sample Usage

Example

/**
 * Hide email from Spam Bots using a shortcode.
 *
 * @param array  $atts    Shortcode attributes. Not used.
 * @param string $content The shortcode content. Should be an email address.
 * @return string The obfuscated email address. 
 */
function wpdocs_hide_email_shortcode( $atts , $content = null ) {
    if ( ! is_email( $content ) ) {
        return;
    }
    return '<a href="' . esc_url('mailto:' . antispambot( $content ) ) . '">' . esc_html( antispambot( $content ) ) . '</a>';
}
add_shortcode( 'email', 'wpdocs_hide_email_shortcode' );

To use this in your WordPress Content area all you have to do it wrap it in a short code.

[email]john.doe@mysite.com[/email]

You can also use this in a plain text widget if you add this filter to your function file as well.

add_filter( 'widget_text', 'shortcode_unautop' );
add_filter( 'widget_text', 'do_shortcode' );

Edited with a contribution from @johnrafferty

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments