-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-to-function-php
36 lines (31 loc) · 1.54 KB
/
add-to-function-php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/*
|--------------------------------------------------------------------------
| Obfuscate All Email Addresses
|--------------------------------------------------------------------------
|
| The following code will obfuscate all email addresses in your content and hardcoded into your theme's files.
| It uses a regular expression to find all mailto links and then replaces them with obfuscated versions using
| WordPress's antispambot() function. This function works by converting characters in the email address into HTML
| entities, which helps prevent spam by making it difficult for bots to harvest email addresses.
|
| This code should be added to your theme's functions.php file.
|
*/
function obfuscate_mailto_links($content) {
// This pattern matches mailto links
$pattern = '/<a\s[^>]*href=["\']mailto:([^@"\']+@[^@"\']+)["\'][^>]*>([^<]+)<\/a>/i';
$content = preg_replace_callback($pattern, function ($matches) {
// Obfuscate the email address
$obfuscated_mail = antispambot($matches[1]);
return '<a href="mailto:' . $obfuscated_mail . '">' . antispambot($matches[2]) . '</a>';
}, $content);
return $content;
}
// Apply this filter to 'the_content' to affect post/page content
add_filter('the_content', 'obfuscate_mailto_links');
// Apply this filter to 'widget_text' to affect text widgets
add_filter('widget_text', 'obfuscate_mailto_links');
// Optionally apply to other filters where email might appear
add_filter('the_excerpt', 'obfuscate_mailto_links');
add_filter('comment_text', 'obfuscate_mailto_links');