Skip to content

Instantly share code, notes, and snippets.

@Camwyn
Created August 6, 2020 16:04
Show Gist options
  • Save Camwyn/1b2c08f6c3e97ff4ee2e95537f2cd02b to your computer and use it in GitHub Desktop.
Save Camwyn/1b2c08f6c3e97ff4ee2e95537f2cd02b to your computer and use it in GitHub Desktop.
<?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
);
}
@Camwyn
Copy link
Author

Camwyn commented Aug 6, 2020

Worth noting that the search/replace is indiscriminate - so beware using this on templates that might contain other links!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment