Last active
December 28, 2017 10:34
-
-
Save donaldallen/7844541 to your computer and use it in GitHub Desktop.
WordPress enqueue scripts with mobile detect.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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