Skip to content

Instantly share code, notes, and snippets.

@michaelschofield
Created July 17, 2014 21:39
Show Gist options
  • Save michaelschofield/692cb6b6d80a534e53ea to your computer and use it in GitHub Desktop.
Save michaelschofield/692cb6b6d80a534e53ea to your computer and use it in GitHub Desktop.
A little WordPress function for micromanaging what, where, and when scripts and styles load.
// Deregister all the things
add_action( 'wp_print_scripts', 'libux_deregister_scripts', 100 );
function libux_deregister_scripts() {
// Ditch jQuery - except on one page that requires it,
// but load that with a CDN.
wp_deregister_script( 'jquery' );
if ( is_page( 'something' ) ) {
wp_enqueue_script( 'jquery', '//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js', true );
wp_enqueue_script('plugin-name', plugins_url( /* url */ ), array('jquery'), true);
}
}
add_action( 'wp_print_styles', 'libux_deregister_styles', 100 );
function libux_deregister_styles() {
// Unless we're on a page where this plugin is used,
// don't by god load that CSS.
if ( !is_page( 'something' ) ) {
wp_deregister_style( 'plugin-name' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment