Last active
March 30, 2022 17:36
-
-
Save rniswonger/f2b1a6a7cdb1bc43580be42f0605ba45 to your computer and use it in GitHub Desktop.
PHP + JS email obfuscation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// encode email link with rot13 | |
$encoded_email_link = str_rot13( 'mailto:' . strtolower( $email_address ) ); | |
?> | |
<script> | |
// decode rot13-encoded text | |
decodeText = text => { | |
return text.replace(/[a-z]/gi, letter => { | |
return String.fromCharCode( | |
letter.charCodeAt(0) + | |
(letter.toLowerCase() <= 'm' ? 13 : -13) | |
) | |
}); | |
}; | |
// decode and write the email anchor tag to the page | |
document.write('<a href=' + | |
' "' + | |
decodeText( '<?php echo $encoded_email_link; ?>' ) + | |
'" ' + | |
'title="<?php _e( 'Email', 'mytheme' ); ?>" ' + | |
'aria-label="<?php _e( 'Link to email address', 'mytheme' ); ?>">' + | |
'<?php _e( 'Email', 'mytheme' ); ?>' + | |
'</a>'); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment