Created
July 12, 2023 18:23
-
-
Save thisbit/2e85b97244c43532c54139cc346f5d3c to your computer and use it in GitHub Desktop.
Old school email obfuscation
This file contains hidden or 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 | |
// Security | |
if ( ! defined( 'ABSPATH' ) ) : exit; endif; | |
function publication_email_link() { | |
global $post; | |
$title = get_the_title($post->ID); | |
$link = get_the_permalink($post->ID); | |
$post_type = get_post_type($post->ID); | |
// link parts (https://spencermortensen.com/articles/email-obfuscation/) | |
$mailto = 'mailto:'; | |
$username = 'info'; | |
$domain = 'paviljon.hr'; | |
$subject = 'Kupnja knjige:' . $title; | |
$contact = '<a href="' . $mailto . $username . '@' . $domain . '?subject=' . $title . '&body=' . $title . '<br>' . $link . '" class="obfuscated">'. $mailto . $username . '[at]' . $domain .'</a>'; | |
$order = '<a href="'. $mailto . $username . '@' . $domain . '?subject=' . $subject . '&body=' . $title . '<br>' . $link . '" class="obfuscated">'. $username . '[at]' . $domain .'</a>'; | |
// if publication post type, link produces slightly different email | |
($post_type != 'publikacija') ? $email_link = $contact : $email_link = $order; | |
?> | |
<script id="obf"> | |
window.addEventListener('DOMContentLoaded', () => { | |
// get the toggle button | |
const revealBook = document.querySelector('.email-button'); | |
if (!revealBook) return; // if not button, no party | |
// build up the dom element to insert | |
let linkWrapper = document.createElement('div'); | |
linkWrapper.classList.add('book-order'); | |
linkWrapper.innerHTML = <?php echo json_encode($email_link); ?>; | |
// inserter | |
function insertAfter(newNode, existingNode) { | |
existingNode.parentNode.insertBefore(newNode, existingNode.nextSibling); | |
} | |
// do the clicking | |
revealBook.addEventListener('click', () => { | |
maiLink = document.querySelector('.book-order'); | |
(typeof(maiLink) != 'undefined' && maiLink != null) ? maiLink.remove() : insertAfter(linkWrapper, revealBook); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_action('wp_head', 'publication_email_link'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made this as a temporary fix for showing emails on the site. Will soon move to https://developers.cloudflare.com/support/more-dashboard-apps/cloudflare-scrape-shield/what-is-email-address-obfuscation/
But maybe it is useful to someone.
Email link does not exist until button i clicked, and the one in js is using html entities. So should work somwhat to prevent from most obvious scrapers.
TO make it work, drop a button on page, and add email-button class to it. Script will do the rest.