Created
August 6, 2020 16:04
-
-
Save Camwyn/1b2c08f6c3e97ff4ee2e95537f2cd02b to your computer and use it in GitHub Desktop.
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 | |
// Put this in your theme's functions.php | |
// Add any title templates you want to modify. | |
$tribe_title_href_templates = [ | |
'events/v2/month/calendar-body/day/calendar-events/calendar-event/title', | |
]; | |
// Loop through them! | |
foreach ( $tribe_title_href_templates as $hook_name ) { | |
// We're using the tribe_template_html:{$hook_name} filter here, see https://theeventscalendar.com/knowledgebase/k/using-template-filters-and-actions/ | |
add_filter( "tribe_template_html:{$hook_name}", function( $html ) { | |
// get the event website url. | |
$url = tribe_get_event_website_url( tribe_get_event( get_the_ID() ) ); | |
// If no URL, bail (return unchanged HTML). | |
if ( ! $url ) { | |
return $html; | |
} | |
// Black magic (really, just look for an href="" and replace what's inside the quotes) | |
$html = preg_replace( | |
'/(href=")[^"]*(")/m', | |
"$1{$url}$2", | |
$html | |
); | |
// Return changed HTML. | |
return $html; | |
}, | |
10 | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worth noting that the search/replace is indiscriminate - so beware using this on templates that might contain other links!