Skip to main content
1 of 2
Sam
  • 2.2k
  • 3
  • 30
  • 59

Writing a function to detect an event

I'm using The Events Calendar to handle events on a site I'm building.

I'd like a PHP include to fire if the user is on an events page (archives, single etc etc).

Here's what I've tested:

/**
 * Detect Tribe Events and add text to top of page
 */
if ( class_exists( 'Tribe__Events__Main' ) ) { // Only triggers if plugin is active

    function is_tribe_calendar() {

        if (tribe_is_event() || tribe_is_event_category() || tribe_is_in_main_loop() || tribe_is_view() || 'tribe_events' == get_post_type() || is_singular( 'tribe_events' ))
            echo '<p style="background: red; color: white; padding: 24px; margin: 0;">Tribe Events is active.</p>';
        }
        
    add_action( 'wp_head' , 'is_tribe_calendar' );

}

This function works perfectly and adds text to the top of the page if the user is on an archive (calendar) or single event.

Based on my test, I then wrote the following:

/**
 * Detect Trive Events and require include
 */
if ( class_exists( 'Tribe__Events__Main' ) ) { // Only triggers if plugin is active

    function is_tribe_calendar() {

        if (tribe_is_event() || tribe_is_event_category() || tribe_is_in_main_loop() || tribe_is_view() || 'tribe_events' == get_post_type() || is_singular( 'tribe_events' ))
            require get_template_directory() . '/inc/tribe-events.php';
        }

    add_action( 'wp_head' , 'is_tribe_calendar' );

}

I don't think the wp_head is the appropriate hook here as I'm wanting to load an include with some custom PHP. Should I even be using a hook for this type of functionality?

Sam
  • 2.2k
  • 3
  • 30
  • 59