Advertisement
TourKick

Remove Gravity View assets from everywhere other than on sin

Dec 31st, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.57 KB | None | 0 0
  1. add_action( 'wp_enqueue_scripts', 'gv_assets_only_on_single_view', 200 );
  2.  
  3. /**
  4.  * Remove Gravity View assets from everywhere other than on single View pages.
  5.  *
  6.  * If script or style source starts with `{MyURL}/wp-content/plugins/gravityview` and we're not on a single View, dequeue it.
  7.  * If you create a custom GravityView plugin of your own and don't want it affected by this snippet, start your plugin
  8.  * folder name with anything other than 'gravityview'.
  9.  *
  10.  * @link https://gist.github.com/cliffordp/12ab4006b43413dc6ebc0a1bc9c1cbbb This snippet on Gist.
  11.  * @link https://pastebin.com/xxAV7jMN This snippet on Pastebin.
  12.  * @link https://docs.gravityview.co/article/575-how-to-remove-all-styles-and-scripts-of-gravityview-from-your-website Article about this topic.
  13.  * @link https://gist.github.com/rafaehlers/506ffd95d7a2b8f4f07fc05a3f4c2a62 Started with and improved upon this snippet.
  14.  */
  15. function gv_assets_only_on_single_view() {
  16.     if ( is_singular( 'gravityview' ) ) {
  17.         return;
  18.     }
  19.  
  20.     $starts_with = trailingslashit( plugins_url() ) . 'gravityview';
  21.  
  22.     // Scripts
  23.     $wp_scripts = wp_scripts();
  24.  
  25.     $scripts = $wp_scripts->registered;
  26.     foreach ( $scripts as &$script ) {
  27.         if (
  28.             is_string( $script->src )
  29.             && 0 === strpos( $script->src, $starts_with )
  30.         ) {
  31.             $wp_scripts->dequeue( $script->handle );
  32.         }
  33.     }
  34.  
  35.     // Styles
  36.     $wp_styles = wp_styles();
  37.  
  38.     $styles = $wp_styles->registered;
  39.     foreach ( $styles as &$style ) {
  40.         if (
  41.             is_string( $style->src )
  42.             && 0 === strpos( $style->src, $starts_with )
  43.         ) {
  44.             $wp_styles->dequeue( $style->handle );
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement