[wp-trac] [WordPress Trac] #60138: make_clickable() processing emails start with www. incorrectly.
WordPress Trac
noreply at wordpress.org
Thu Feb 15 06:01:13 UTC 2024
#60138: make_clickable() processing emails start with www. incorrectly.
-----------------------------------------+------------------------------
Reporter: olyahryhorenko17 | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Formatting | Version:
Severity: trivial | Resolution:
Keywords: has-patch changes-requested | Focuses:
-----------------------------------------+------------------------------
Comment (by ikpnqgcr55):
To address the issue where `make_clickable()` processes email addresses
starting with "www." incorrectly by treating them as FTP addresses and
appending "https://" instead of "mailto:", we can create a custom filter
to handle these cases correctly. Below is the modified approach to ensure
that such email addresses are correctly converted into clickable mailto
links:
```php
// Custom filter to modify make_clickable() behavior for email addresses
function custom_make_clickable( $text ) {
// Define a regular expression pattern to identify email addresses
$pattern = '/(?:^|\s)(www\.\S+@[\w.-]+)/i';
// Use a callback function to replace matched email addresses with
mailto links
$text = preg_replace_callback( $pattern,
'custom_make_clickable_callback', $text );
// Return the modified text
return $text;
}
// Callback function to create mailto links for email addresses starting
with "www."
function custom_make_clickable_callback( $matches ) {
$email = esc_url( 'mailto:' . $matches[1] );
return '<a href="' . $email . '">' . $matches[1] . '</a>';
}
// Apply the custom make_clickable filter
add_filter( 'make_clickable', 'custom_make_clickable', 10, 1 );
```
Explanation:
- We define a custom filter function `custom_make_clickable()` to modify
the behavior of `make_clickable()` for email addresses.
- Inside this filter function, we use a regular expression pattern to
identify email addresses that start with "www.".
- We then use `preg_replace_callback()` to replace each matched email
address with a mailto link.
- The `custom_make_clickable_callback()` function generates the mailto
link for each matched email address.
- Finally, we apply our custom `make_clickable` filter using
`add_filter()`.
By applying this custom filter, email addresses starting with "www." will
be correctly recognized and converted into clickable mailto links instead
of being treated as URLs with "https://" appended. This ensures that the
behavior of `make_clickable()` aligns with the desired functionality.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/60138#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list