Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
TEC archive pages (not single events): Add a second export/subscribe button specific to Google Calendar
<?php
/**
* TEC archive pages (not single events): Add a second export/subscribe button specific to Google Calendar. Screenshot: https://cl.ly/2j2d2d1B3s1E
*
* Beware: this does not respect Event Categories and such. It's just the same URL no matter which event archive page you are on.
* Based on maybe_add_link() from TEC v4.5.9
*
* @link https://gist.github.com/cliffordp/96759ce94fedb344509ba2a02fc334ce
* @link https://tribe.uservoice.com/forums/195723-feature-ideas/suggestions/6430110-subscribe-to-calendar-with-google-calendar
*/
add_action( 'tribe_events_after_footer', 'cliff_output_dedicated_google_export_button_archive_pages', 9 );
function cliff_output_dedicated_google_export_button_archive_pages() {
global $wp_query;
$show_ical = apply_filters( 'tribe_events_list_show_ical_link', true );
if ( ! $show_ical ) {
return;
}
if ( tribe_is_month() && ! tribe_events_month_has_events() ) {
return;
}
if ( is_single() || empty( $wp_query->posts ) ) {
return;
}
$ical_link = tribe_get_ical_link();
// Because Google Calendar does not support https URLs via cid=...
$count = 1;
$ical_link = str_replace( 'https://', 'webcal://', $ical_link, $count );
$gcal_subscribe = sprintf( 'https://www.google.com/calendar/render?cid=%s', urlencode( $ical_link ) );
$text = '+ Subscribe via Google Calendar';
$title = 'Clicking this link will open your Google Calendar, which will prompt you to add this calendar as one of your subscriptions.';
$output = printf(
'<a class="tribe-events-gcal tribe-events-button" style="margin-right: 0;" title="%s" href="%s" target="_blank">%s</a>',
esc_html( $title ),
esc_url( $gcal_subscribe ),
esc_html( $text )
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment