Skip to main content
2 of 2
Updated question.
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 an include to fire if the user is on an events page (archives, single etc etc). My include has a handful of custom hooks which handle enqueuing, dequeuing and inserting custom HTML.

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