Only load JS/CSS for Google Reviews when needed
-
Hi,
I want to remove the JS/CSS scripts plugin Google Reviews from all pages except frontpage. How to do it?
Functions
wp_dequeue_script( $handle );
andwp_dequeue_style( $handle );
not working.Thank for help.
-
What hook and priority are you using for this? Share your whole relevant code.
But really, the plugin shouldn’t load its resources unless they’re needed.
- This reply was modified 8 months, 3 weeks ago by Gal Baras.
I tried again and succeeded.
Plugin generate scripts on all pages:
<script type="text/javascript" defer="defer" src="localhost/wp-content/plugins/widget-google-reviews/assets/js/public-main.js?ver=3.4" id="grw-public-main-js-js"></script> <link rel="stylesheet" id="grw-public-main-css-css" href="localhost/wp-content/plugins/widget-google-reviews/assets/css/public-main.css?ver=3.4" type="text/css" media="all">
Below functions solve the problem:
function perla_remove_scripts() { if ( !is_front_page() ) { wp_dequeue_script( 'grw-public-main-js' ); } } add_action( 'wp_print_scripts', 'perla_remove_scripts', PHP_INT_MAX, 0 ); function perla_remove_styles() { if ( !is_front_page() ) { wp_dequeue_style( 'grw-public-main-css' ); } } add_action( 'wp_enqueue_scripts', 'perla_remove_styles', PHP_INT_MAX, 0 );
Topic to be closed.
Hi,
Thank you for share a good idea. In the next version we add special option for demand loads assets. With this option JS/CSS resources will be enqueue only on pages where widget shortcode is.Thanks!
You can also add a true/false filter, e.g.
grw_enqueue_assets
, to override the decision whether to enqueue or not, giving site developers greater control.Hi @galbaras,
Yes, it’s good idea. Could you write some little bit more lines how this filter would works?
Thanks!
Sure, why not.
Let’s say you have a function
Assets::should_enqueue_assets()
, which returns true if assets should be loaded. You can have this value stored in a variable, e.g.$enqueue_assets
, and then end the function with:return apply_filters( 'grw_enqueue_assets',
$enqueue_assets
);You can then use this function in
Assets::register_styles()
,Assets::register_scripts()
,Assets::enqueue_public_styles()
andAssets::
enqueue_public
_scripts() like so:if ( ! should_enqueue_assets() ) return;
Within
should_enqueue_asset()
, you can usehas_shortcode()
to determine whether a shortcode is used, and check the active sidebar(s) for relevant widgets.Hi @galbaras,
Thank you for your great code, it gave us food for thought.
However, the function has_shortcode can makes some sideeffect when user uses a site/page builder and put shortcode to unspecific places (header, rows, popups etc). In this case we do not know which exactly content we should pass in that function.
Anyway, thanks for that code!
I can see the problem here, but you can cover many WordPress installations using compatibility for the Block Editor and Classic Editor. Over time, this can be extended, if there’s enough demand.
Another approach would be to provide plugin settings for excluding post IDs and/or page types.
Hi @galbaras,
Thank you for explanation.
However, in the next version we implemented another solution. We introduced new option (load js/css on demand) and if it’s true, enqueue_scripts/enqueue_styles calls when shortcode and/or widget initialized.
But it has some side effect, js/css assets load and appear at the end of the HTML page.
Thanks!
Since the comment system is typically accessed towards the bottom of the page, deferred selective loading of assets is a great idea, although I hope the decision about what to load will be made in PHP, not JS, to allow subsequent page optimisation.
Any idea when the next version will be released?
Hi,
Yes, sure, it’s a PHP side, this check occurs before shortcode or widget initialization.
New release should be on next week
Thanks!
- The topic ‘Only load JS/CSS for Google Reviews when needed’ is closed to new replies.