Skip to content

Instantly share code, notes, and snippets.

@ethangardner
Last active March 11, 2016 20:37
Show Gist options
  • Save ethangardner/fef48abd4af738280957 to your computer and use it in GitHub Desktop.
Save ethangardner/fef48abd4af738280957 to your computer and use it in GitHub Desktop.
Loading scripts in functions.php and removing from header.php (recommended)
<?php
/**
* We've stripped the script tags from header.php and relocated them to functions.php
*/
// loads the external scripts
function wptheme_load_scripts() {
wp_deregister_script( 'jquery' ); // deregisters the version of jQuery shipped with WP so you can use Google's CDN
wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js' );
wp_enqueue_script( 'jquery' ); // actually loads it on the page
wp_enqueue_script( 'wptheme-site', get_template_directory_uri() . '/js/modernizr.js', array(), '2' );
wp_enqueue_script( 'wptheme-site', get_template_directory_uri() . '/js/script.js', array( 'jquery' ), '2' );
}
add_action( 'wp_enqueue_scripts', 'wptheme_load_scripts');
// loads inline scripts at the very bottom when the wp_head() function is called in header.php
function wptheme_load_conditional_scripts() {
?>
<!--[if lte IE 8]>
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/html5shiv.min.js"></script>
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/respond.min.js"></script>
<![endif]-->
<?php
}
add_action( 'wp_head', 'wptheme_load_conditional_scripts', PHP_INT_MAX );
/**
* Other functions go here
*/
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<title><?php wp_title( '|', true, 'right' ); ?></title>
<?php wp_head(); ?>
</head>
<?php
// Rest of the template would go here...
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment