Skip to content

Instantly share code, notes, and snippets.

@NichlasB
Created June 27, 2017 18:19
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 NichlasB/03dfeed69d05ae3f3ebfe4ede5744e88 to your computer and use it in GitHub Desktop.
Save NichlasB/03dfeed69d05ae3f3ebfe4ede5744e88 to your computer and use it in GitHub Desktop.
Conditionally Load WordPress Plugin Assets
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* dequeue-assets.php
* Resource: http://orbitingweb.com/blog/preventing-plugins-from-loading-js-css-on-all-pages/
*/
//* Finding handles for plugins
function cf_display_script_handles() {
global $wp_scripts;
if (current_user_can( 'manage_options' ) && ! is_admin() ) { # Only load when user is admin
foreach( $wp_scripts->queue as $handle ) :
$obj = $wp_scripts->registered [$handle];
echo $filename = $obj->src;
echo ' : <b>Handle for this script is:</b> <span style="color:green"> '. $handle .'</span><br/><br/>';
endforeach;
}
}
add_action( 'wp_print_scripts', 'cf_display_script_handles' );
/**
* Conditionally load plugins
*/
//* Dequeue Let's Review assets
function cf_conditionally_load_lets_review() {
if ( ! is_singular( 'post' ) || ! has_tag( 'review' ) ) {
wp_dequeue_script( 'lets-review-js-ext' );
wp_dequeue_script( 'lets-review-js' );
}
}
add_action( 'wp_enqueue_scripts', 'cf_conditionally_load_lets_review' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment