Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #753421

    For along time now I have used Javascript to obfuscate email address – so they can’t be read by spambots.

    
    <a href="javascript:window.location.href ='mailto:'+['name','domain.com'].join('@')+'?subject='+'Email Subject'">Click to email</a>
    

    More recently on Enfolds the JS had to be placed in side an AVIA [av_codeblock] [/av_codeblock]

    
    [av_codeblock wrapper_element='' wrapper_element_attributes='' escape_html='' deactivate_shortcode='' deactivate_wrapper='']<a href="javascript:window.location.href ='mailto:'+['name','domain.com'].join('@')+'?subject='+'Email Subject'">Click to email</a>[/av_codeblock]
    

    As this is a [shortcode] I would then copy this short code to include as an inline element.

    <ul>
    <li>P:  00 0000 0000</li>
    <li>M: 0000 000 000</li>
    <li>E: [av_codeblock wrapper_element='' wrapper_element_attributes='' escape_html='' deactivate_shortcode='' deactivate_wrapper='']<a href="javascript:window.location.href ='mailto:'+['name','domain.com'].join('@')+'?subject='+'Email Subject'">Click to email</a>[/av_codeblock]</li>
    </ul>

    However I noticed that every time the AVIA layout element was loaded and edited and updated the [Shortcode] and JS would disappear.

    I got fed up and asked our programmers to write this nice little [Shortcode]

    Please add the following in your child-theme functions.php file.
    Set your default values:
    ’email’ => (Email address hidden if logged out) ‘,
    ‘subject’ => ‘Email Website Enquiry’,
    ‘text’ => ‘Click to email’,
    ‘class’ => ”
    On your page where you want insert and email address add the shortcode:

    EXAMPLE 1: custom email address with default values for the other shortcodes variables

    
    [obfuscate_email email=" (Email address hidden if logged out) " ]
    

    EXAMPLE 2: Fully custom to override default values.

    
    [obfuscate_email email=" (Email address hidden if logged out) " subject="Website - John Smith - Email Enquiry" text=" Email John Smith"]
    

    CUSTOM CSS CLASS
    You can add class=”” to the shortcode to add your own custom CSS class.
    EXAMPLE 3: CSS CLASS

    
    [obfuscate_email email=" (Email address hidden if logged out) " subject="Website - John Smith - Email Enquiry" text=" Email John Smith" class="custom_class"]
    

    now watch the magic happen!

    
    /*-------------------------------------------------------------------------------
    	SHORTCODE  - obfuscate email
    	[obfuscate_email email=" (Email address hidden if logged out) " subject="Test Subject" text="Click to Email" class="custom_class"]
    -------------------------------------------------------------------------------*/
    
    function obfuscate_email_shortcode( $atts ){
    
    //default values
        extract( shortcode_atts([
            'email' => ' (Email address hidden if logged out) ',
            'subject' => 'Email Website Enquiry',
            'text' => 'Click to email',
            'class' => ''        
        ], $atts ) );
        
    //split the email address so it can't be read by spam bots
        $email_parts = explode( '@', $email, 2 );
    
    //recreate the js url
        $url = sprintf(
            "javascript:window.location.href='mailto:' + [ '%s', '%s' ].join('@') + '?subject=%s'",
            $email_parts[0],
            $email_parts[1],
            $subject
        );
    //print to screen
        return sprintf(
            '<a href="%s" class="%s">%s</a>',
            $url,
            $class,
            $text
        );
    
    }
    add_shortcode( 'obfuscate_email', 'obfuscate_email_shortcode' );
    

    Please Share!

    • This topic was modified 7 years, 8 months ago by HuxburyQuinn. Reason: code sections didn't look right on screen
    #753551

    Hey HuxburyQuinn,

    Thank you for sharing your solution, we really appreciate it!
    I’m sure it will also help others who are trying to achieve the same :)

    Best regards,
    Vinay

    #914783

    Update
    How to include a Body with multiple lines in a mailto: link

    Replace the //default values code section with the code below:

    
    //default values
        extract( shortcode_atts([
            'email' => ' (Email address hidden if logged out) ',
            'subject' => 'Website Email Enquiry',
            'text' => 'Email Us',
            'body' => '',
            'class' => 'custom_class'        
        ], $atts ) );
    

    Then replace the sprintf output with the following:

    
    $url = sprintf(
            "javascript:window.location.href='mailto:' + [ '%s', '%s' ].join('@') + '?subject=%s&body=%s'",
            $email_parts[0],
            $email_parts[1],
            $subject,
            str_replace( '<br />', '%250D%250A', $body )
        );
    

    Explination
    Stackoverflow – insert a line break in mailto body
    \r\n and %0D%0A within the shortcode body=”line 1 example\r\nline 2 example” OR body=”line 1 example%0D%0Aline 2 example” did not work within the shortcode and in modern email applications.

    After troubleshooting for 3 hours – I worked out that urlencode($body) honoured line returns. However the body output had replaced spaces with a plus’s ‘+’.

    Not very readable.

    So in the updated code we search for <br /> in the body content and replace %0D%0A with the urlencode format %250D%250A to maintain the line returns.

    • This reply was modified 6 years, 8 months ago by HuxburyQuinn.
    #915164

    Hi HuxburyQuinn,

    Thanks for sharing, much appreciated :-)

    Best regards,
    Rikard

    #918781

    UPDATE

    PHP extract() function is being depreciated – so I have updated the code below

    https://externals.io/message/100637

    /*-------------------------------------------------------------------------------
    	SHORTCODE  - obfuscate email
    	[obfuscate_email email=" (Email address hidden if logged out) " subject="Test Subject" body="Message with line returns
    	line 2
    	line 3" text="Click to Email" class="custom_class"]
    -------------------------------------------------------------------------------*/
    
    function obfuscate_email_shortcode( $atts ){
    
    //default values
    	$atts = shortcode_atts([
    		'email' => ' (Email address hidden if logged out) ',
    		'subject' => 'Website Email Enquiry',
    		'text' => 'Email Us',
    		'body' => '',
    		'class' => 'email_hide'
    	], $atts );
    
    //split the email address so it can't be read by spam bots
    	$email_parts = explode( '@', $atts['email'], 2 );
    
    //recreate the js url
    	$url = sprintf(
    		"javascript:window.location.href='mailto:' + [ '%s', '%s' ].join('@') + '?subject=%s&body=%s'",
    		$email_parts[0],
    		$email_parts[1],
    		$atts['subject'],
    		str_replace( '<br />', '%250D%250A', $atts['body'] )
    	);
    
    //print to screen
    	return sprintf(
    		'<a href="%s" class="%s">%s</a>',
    		$url,
    		$atts['class'],
    		$atts['text']
    	);
    
    }
    add_shortcode( 'obfuscate_email', 'obfuscate_email_shortcode' );
    
    #918846

    Hi,

    Thanks for sharing this solution. It would really be helpful to other enfold users who needs to obfuscate email address :)

    Best regards,
    Nikko

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.