最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - Writing a function to detect an event

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Jun 14, 2019 at 17:13 Sam asked Jun 14, 2019 at 16:59 SamSam 2,1963 gold badges30 silver badges59 bronze badges 2
  • It depends on what exactly you're trying to do, which you haven't really described. What is the PHP include? What does it do? – Jacob Peattie Commented Jun 14, 2019 at 17:09
  • I've updated my question :) – Sam Commented Jun 14, 2019 at 17:13
Add a comment  | 

1 Answer 1

Reset to default 1

My include has a handful of custom hooks which handle enqueuing, dequeuing and inserting custom HTML.

I'm still not 100% certain what you mean, but if this code is normal WordPress code, with functions and add_action(), and you only want those hooks to run on these events pages, then the proper way to do this would be to include that file into your theme's functions file or plugin's main plugin file, and then use your condition inside those hooks.

So in your functions file, just include your other file (I've used get_theme_file_path() instead of get_template_directory(), which is the preferred method these days):

require get_theme_file_path( 'inc/tribe-events.php' );

Then inside that file you would have your hooks like this:

function my_hooked_function( $arg ) {
    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' )
    ) {
        // Do thing.
    }
}
add_action( 'hook_name', 'my_hooked_function' );

function my_second_hooked_function( $arg ) {
    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' )
    ) {
        // Do thing.
    }
}
add_action( 'another_hook_name', 'my_second_hooked_function' );

Or, to reduce the amount of code, you could define your own conditional function that you can re-use:

function is_tribe_calendar() {
    if (
        return tribe_is_event() || 
        tribe_is_event_category() || 
        tribe_is_in_main_loop() || 
        tribe_is_view() || 
        'tribe_events' == get_post_type() || 
        is_singular( 'tribe_events' )
    ) {
        return true;
    } else {
        return false;
    }
}

function my_hooked_function( $arg ) {
    if ( is_tribe_calendar() ) {
        // Do thing.
    }
}
add_action( 'hook_name', 'my_hooked_function' );

function my_second_hooked_function( $arg ) {
    if ( is_tribe_calendar() ) {
        // Do thing.
    }
}
add_action( 'another_hook_name', 'my_second_hooked_function' );
发布评论

评论列表(0)

  1. 暂无评论