Created
June 27, 2017 18:19
-
-
Save NichlasB/03dfeed69d05ae3f3ebfe4ede5744e88 to your computer and use it in GitHub Desktop.
Conditionally Load WordPress Plugin Assets
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 | |
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