• Resolved Alwin

    (@wp-opti)


    Hello, I like to publish my email address on my website but want to protect it from spammers. I have read about the build in function called antispambot. I like to use that instead of a plugin.

    https://codex.wordpress.org/Function_Reference/antispambot

    I don’t know exactly how to set this up. Maybe like this:

    1 – add this code to my function.php file:

    
    /**
     * 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 wpcodex_hide_email_shortcode( $atts , $content = null ) {
    	if ( ! is_email( $content ) ) {
    		return;
    	}
    
    	$content = antispambot( $content );
    
    	$email_link = sprintf( 'mailto:%s', $content );
    
    	return sprintf( '<a href="%s">%s</a>', esc_url( $email_link, array( 'mailto' ) ), esc_html( $content ) );
    }
    add_shortcode( 'email', 'wpcodex_hide_email_shortcode' );
    

    2 – Change the email links on my contact page into a sort code[email]john.doe@mysite.com[/email]

    Is that all?

    Thank you very much for your help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Stefano

    (@madking-web-design)

    Hey @alwin !
    Anti-SpamBot plugin, easily can do the trick for you. The plugin using built-in WordPress Codex functionality.
    Here are some examples in case that you want to build your own functionality!

    Thread Starter Alwin

    (@wp-opti)

    Hello Stefano,

    I like to do it without needing an (extra) plugin.. 🙂

    The link you send me is the same as in my own post.

    How to set it up, that is my question. First add the code to my functions.php and then use the short codes instead of my usual mailto links?

    ThaNk you,
    Alwin

    Stefano

    (@madking-web-design)

    Exactly, you need to add the code into your theme’s function.php
    In that case I would suggest to use a childtheme and make sure also to have a recent backup and FTP(or SFTP) access, just in case something goes wrong.

    Thread Starter Alwin

    (@wp-opti)

    I have added this code to my functions.php in my child theme:

    /**
    * 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 wpcodex_hide_email_shortcode( $atts , $content = null ) {
    if ( ! is_email( $content ) ) {
    return;
    }

    $content = antispambot( $content );

    $email_link = sprintf( ‘mailto:%s’, $content );

    return sprintf( ‘%s‘, esc_url( $email_link, array( ‘mailto’ ) ), esc_html( $content ) );
    }
    add_shortcode( ’email’, ‘wpcodex_hide_email_shortcode’ );

    I have not changend anything in this code. That is correct?

    Then I have added the shortcode in my wordpress text area.

    Now when I look at the website page I see my email address and when I put the mouse over the email address I still see normal mailto:info@mydomain.com link under in the browser. Is that how it suposed to be?

    When I look in the source code I see that my email address is indeed encoded.

    Everything okay like this?

    Stefano

    (@madking-web-design)

    If it’s encoded, then I thing mission accomplished..Should appear as a normal email address to anyone using web browser, but most bots will not be able to identify it.

    Thread Starter Alwin

    (@wp-opti)

    Great! Thank you for your help!

    🙂

    Stefano

    (@madking-web-design)

    Cool! 😉

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘WordPress antispambot?’ is closed to new replies.