Skip to content

Instantly share code, notes, and snippets.

@donaldallen
Last active December 28, 2017 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donaldallen/7844541 to your computer and use it in GitHub Desktop.
Save donaldallen/7844541 to your computer and use it in GitHub Desktop.
WordPress enqueue scripts with mobile detect.
<?php
add_action('wp_enqueue_scripts', function() {
if ( ! is_admin()) {
// Get rid of the default jQuery
wp_deregister_script('jquery');
// For non-mobile devices
if ( ! $wp_is_mobile()) {
wp_register_script('jquery-js', JS_PUBLIC_URI . 'vendor/jquery/jquery.min.js', array(), '', false);
wp_register_script('modernizr', JS_PUBLIC_URI . 'vendor/modernizr/modernizr.min.js', array(), '', false);
wp_register_script('main-js', JS_PUBLIC_URI . 'main.js', array('jquery-js'), '', true);
wp_register_style('stylesheet', get_stylesheet_directory_uri() . '/style.css', array(), '', 'all');
if ($is_IE) {
wp_register_script('html5shiv', 'http://html5shiv.googlecode.com/svn/trunk/html5.js' , false, true);
wp_enqueue_script('html5shiv');
}
wp_enqueue_style('stylesheet');
wp_enqueue_script('jquery-js');
wp_enqueue_script('modernizr');
wp_enqueue_script('main-js');
} else if ($wp_is_mobile()) {
wp_register_script('jquery-js', JS_PUBLIC_URI . 'vendor/jquery/jquery.min.js', array(), '', false);
wp_register_script('mobile-js', JS_PUBLIC_URI . 'mobile.js', array('jquery-js'), '', true);
wp_register_style('stylesheet', get_stylesheet_directory_uri() . '/style.css', array(), '', 'all');
wp_enqueue_style('stylesheet');
wp_enqueue_script('jquery-js');
wp_enqueue_script('mobile-js');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment