The Events Calendar + ACF: Select2 Conflict

I just noticed this issue today and thought I’d share a fix for it. Both Advanced Custom Fields and Modern Tribe’s The Events Calendar are enqueuing (globally) a version of Select2. Since they are not the same version, this will break some UI for ACF. If either of them are justified in loading select2 on every admin page, it’s ACF since you can literally create custom fields on just about every WordPress admin screen. TEC really only needs select2 on TEC related pages.

In my opinion, you should setup your plugins so that then only enqueue scripts/styles whenever they are needed. I still think ACF could do this only when certain field types are being used.

When I searched the web for this issue, I noticed a few posts on TEC forums that dated back to 2016, however this appears to still be a problem. Plus all of those posts were closed to replies, hence this blog post.

Anyway, the easiest fix in this situation was to disable TEC version when it wasn’t needed, on non-TEC related pages.


<?php
/**
* Disable Tribe Select2 on non-tribe admin pages
*/
function _theme_disable_tribe_select2() {
$screen = get_current_screen();
if ( 'tribe_events' === $screen->id ) {
return;
}
$tribe_post_types = array(
'tribe_events',
'tribe_venue',
);
if ( in_array( $screen->post_type, $tribe_post_types ) ) {
return;
}
wp_deregister_script( 'tribe-select2' );
}
add_action( 'admin_enqueue_scripts', '_theme_disable_tribe_select2', 99 );

I hope this saves someone some time from chasing down this bug.

One Response

  1. Sumon

    It’s working, Thanks

Write a Comment