Created
April 14, 2025 18:57
-
-
Save presswizards/9b6fad1fe635beb0d11ebd22ae22a141 to your computer and use it in GitHub Desktop.
Inject and link email address only on user interaction
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
| // Add this normal link with this specific ID to your page anywhere you want it: | |
| <a id="email-link" href="#">Email Us</a> | |
| // WordPress Code Snippet to add: | |
| function call_email_js_function() { | |
| echo "<script> | |
| function setupEmailInjection() { | |
| document.addEventListener('DOMContentLoaded', function () { | |
| function injectEmail() { | |
| const user = 'hello'; | |
| const domain = 'example.com'; | |
| const email = `${user}@${domain}`; | |
| const link = document.getElementById('email-link'); | |
| if (link) { | |
| link.href = `mailto:${email}`; | |
| // Uncomment this line to replace Email Us with the actual email address, not really needed: | |
| // link.textContent = email; | |
| } | |
| ['click', 'mousemove', 'keydown', 'touchstart', 'scroll'].forEach(event => | |
| window.removeEventListener(event, injectEmail) | |
| ); | |
| } | |
| ['click', 'mousemove', 'keydown', 'touchstart', 'scroll'].forEach(event => | |
| window.addEventListener(event, injectEmail, { once: true }) | |
| ); | |
| }); | |
| } | |
| setupEmailInjection(); | |
| </script>"; | |
| } | |
| add_action('wp_footer', 'call_email_js_function'); | |
| // Non-WordPress Version: Just add this JS to your footer: | |
| <script> | |
| function setupEmailInjection() { | |
| document.addEventListener('DOMContentLoaded', function () { | |
| function injectEmail() { | |
| const user = 'hello'; | |
| const domain = 'example.com'; | |
| const email = `${user}@${domain}`; | |
| const link = document.getElementById('email-link'); | |
| if (link) { | |
| link.href = `mailto:${email}`; | |
| // Uncomment this line to replace Email Us with the actual email address, not really needed: | |
| // link.textContent = email; | |
| } | |
| ['click', 'mousemove', 'keydown', 'touchstart', 'scroll'].forEach(event => | |
| window.removeEventListener(event, injectEmail) | |
| ); | |
| } | |
| ['click', 'mousemove', 'keydown', 'touchstart', 'scroll'].forEach(event => | |
| window.addEventListener(event, injectEmail, { once: true }) | |
| ); | |
| }); | |
| } | |
| setupEmailInjection(); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment