Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active December 31, 2019 18:08
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 cliffordp/12ab4006b43413dc6ebc0a1bc9c1cbbb to your computer and use it in GitHub Desktop.
Save cliffordp/12ab4006b43413dc6ebc0a1bc9c1cbbb to your computer and use it in GitHub Desktop.
Remove Gravity View assets from everywhere other than on single View pages.
<?php
add_action( 'wp_enqueue_scripts', 'gv_assets_only_on_single_view', 200 );
/**
* Remove Gravity View assets from everywhere other than on single View pages.
*
* If script or style source starts with `{MyURL}/wp-content/plugins/gravityview` and we're not on a single View, dequeue it.
* If you create a custom GravityView plugin of your own and don't want it affected by this snippet, start your plugin
* folder name with anything other than 'gravityview'.
*
* @link https://gist.github.com/cliffordp/12ab4006b43413dc6ebc0a1bc9c1cbbb This snippet on Gist.
* @link https://pastebin.com/xxAV7jMN This snippet on Pastebin.
* @link https://docs.gravityview.co/article/575-how-to-remove-all-styles-and-scripts-of-gravityview-from-your-website Article about this topic.
* @link https://gist.github.com/rafaehlers/506ffd95d7a2b8f4f07fc05a3f4c2a62 Started with and improved upon this snippet.
*/
function gv_assets_only_on_single_view() {
if ( is_singular( 'gravityview' ) ) {
return;
}
$starts_with = trailingslashit( plugins_url() ) . 'gravityview';
// Scripts
$wp_scripts = wp_scripts();
$scripts = $wp_scripts->registered;
foreach ( $scripts as &$script ) {
if (
is_string( $script->src )
&& 0 === strpos( $script->src, $starts_with )
) {
$wp_scripts->dequeue( $script->handle );
}
}
// Styles
$wp_styles = wp_styles();
$styles = $wp_styles->registered;
foreach ( $styles as &$style ) {
if (
is_string( $style->src )
&& 0 === strpos( $style->src, $starts_with )
) {
$wp_styles->dequeue( $style->handle );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment