Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active February 21, 2020 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cliffordp/b3745850fd2ab2aab1d4f104ed05ef93 to your computer and use it in GitHub Desktop.
Save cliffordp/b3745850fd2ab2aab1d4f104ed05ef93 to your computer and use it in GitHub Desktop.
The Events Calendar (v2 views): Add Thickbox to single event's featured image.
<?php
/**
* The Events Calendar (v2 views): Add Thickbox to single event's featured image (also make smaller and centered).
*
* @link https://gist.github.com/cliffordp/b3745850fd2ab2aab1d4f104ed05ef93 This snippet.
* @link https://share.getcloudapp.com/E0uqlBJp Quick demo video.
*
* @see cliff_tec_featured_image_thickbox_class()
*/
function cliff_tec_featured_image_thickbox() {
// Protect against fatal if not on TEC v5.0+
if ( ! class_exists( '\Tribe\Events\Views\V2\Template_Bootstrap' ) ) {
return;
}
// If on single event page
/** @var \Tribe\Events\Views\V2\Template_Bootstrap $template */
$template = tribe( \Tribe\Events\Views\V2\Template_Bootstrap::class );
if ( ! $template->is_single_event() ) {
return;
}
// If event has a featured image
if ( empty( tribe_event_featured_image() ) ) {
return;
}
// Enqueue thickbox
wp_enqueue_script( 'thickbox' );
wp_enqueue_style( 'thickbox' );
// Wrap featured image with full size image in thickbox
add_action( 'wp_footer', 'cliff_tec_featured_image_thickbox_class' );
}
add_action( 'wp_enqueue_scripts', 'cliff_tec_featured_image_thickbox' );
/**
* Convert the featured image to a clickable thickbox and add styling.
*
* If you don't want to add the styling, you may want to change the single event's featured image size in this file:
* /wp-content/plugins/the-events-calendar/src/views/blocks/featured-image.php
*
* @see cliff_tec_featured_image_thickbox() Runs before this function.
*/
function cliff_tec_featured_image_thickbox_class() {
$img_url = tribe_event_featured_image( null, 'full', false, false );
$img_wrap = sprintf( '<a class="thickbox" href="%s"></a>', $img_url );
// Thickbox already enqueues jQuery.
// We make the Featured Image smaller and centered (but keep loading the 'full' size since another size, like 'medium' would be an additional HTTP call).
?>
<script>
jQuery( document ).ready( function() {
let tec_featured_img_wrap = jQuery( '.tribe-events-event-image' );
tec_featured_img_wrap
.find( 'img' )
.wrap( '<?php echo $img_wrap; ?>' )
;
tec_featured_img_wrap
.css( 'max-width', '400px' )
.css( 'margin-left', 'auto' )
.css( 'margin-right', 'auto' )
;
} );
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment