WordPress+Genesis+Beaver Builder: Conditionally Enqueue Scripts to Band-aid Misbehaving Plugins

Ran into a problem with an external piece of code on a WordPress site that uses Genesis Framework + Beaver Builder (BB). The BB editor would fail to open or be slow to open. Usually a page refresh would fix this, and most BB functions worked just fine. The exception was clicking any BB link that opened a popup version of the WordPress media chooser window. The window would fail to open. After some troubleshooting, I was able to consistently resolve this issue by removing the script reference. I tried reinserting the script reference at various locations in the html, to no avail. What worked, however, was to selectively remove the script reference while in BB edit mode. Below is the code I used to fix this (added into functions.php of our site’s child theme). Beaver Builder appends a parameter of “fl_builder” whenever you’re editing a page. This allows you enqueue the script only when the “fl_builder” parameter is missing.

/* Selectively include externally referenced script only when Beaver Builder editor is inactive */
add_action( 'wp_enqueue_scripts', 'gk_enqueue_scripts_styles' );
function gk_enqueue_scripts_styles() {
if (isset($_GET['fl_builder'])) {
} else {
wp_enqueue_script( 'gk-external-script', 'https://externalscript.domain/ABCDEF/', array(), null, true );
}
}