Enqueue Theme Navigation Scripts Conditionally

A lot of themes include some kind of responsive navigation script. While it feels wasteful to delete that entirely, it feels even more so to load scripts you’re not using, so here’s how I load my theme navigation files only if SuperSide Me is not active:

<?php
// you can add this to your theme's functions.php file, but do not include the
// opening tag!
add_action( 'wp_enqueue_scripts', 'prefix_load_scripts' );
function prefix_load_scripts() {
if ( function_exists( 'supersideme_has_content' ) && supersideme_has_content() ) {
return;
}
wp_enqueue_script( 'leaven-responsive-menu', get_stylesheet_directory_uri() . '/js/responsive-menu.js', array( 'jquery' ), '1.0.0', true );
$output = array(
'mainMenu' => 'Menu',
'subMenu' => 'Menu',
);
wp_localize_script( 'leaven-responsive-menu', 'LeavenL10n', $output );
}
view raw enqueue.php hosted with ❤ by GitHub

We check to see if SuperSide Me is active, and also to see if the menu panel can be generated. If it can, then there is no need to load the theme’s responsive navigation script.

Return to SuperSide Me
Bars Code Check chevron-down chevron-left chevron-right chevron-up Envelope Laptop Search Times Facebook GitHub Google Plus Instagram LinkedIn Pinterest Twitter WordPress Logo Wordpress Simple