36

How to Encode Email Addresses with an Oxygen Custom Component

Powered by WPCodeBox

This experimental Oxygen Custom Component registers a snippet that encodes email addresses using HTML entities, making them harder for bots to parse and helping to prevent email harvesting.


<?php 

add_action('plugins_loaded', function() {
    
    class EncodeEmail extends OxyEl {
    
        function init() {
        }
    
        function afterInit() {
            $this->removeApplyParamsButton();
        }
    
        function name() {
            return 'Obfuscated Email';
        }
        
        function slug() {
            return "obfuscated-email";
        }
        
        function icon() {
    		return CT_FW_URI . '/toolbar/UI/oxygen-icons/add-icons/loginform.svg';
        }
    	
        function controls() {
       
        $email_control = $this->addOptionControl(
	    array(
                    "type" => 'textfield', 
		    "name" => 'Enter Email',
		    "slug" => 'email'
            )
        );
        
                $email_control->rebuildElementOnChange();
        }
        
        function render($options, $defaults, $content) {
            
            ?>
                <?php echo $this->encode_email_address($options['email']); ?>
            <?php
        }
        
        /**
         * Encode an email address to display on your website
         */
        function encode_email_address( $email ) {
        
             $output = '';
        
             for ($i = 0; $i < strlen($email); $i++) 
             { 
                  $output .= '&#'.ord($email[$i]).';'; 
             }
             
             $output = $output;
        
             return $output;
        }
    }
    
    new EncodeEmail();
});

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.