Skip to content

Instantly share code, notes, and snippets.

@rafaehlers
Last active May 11, 2021 18:42
Show Gist options
  • Save rafaehlers/506ffd95d7a2b8f4f07fc05a3f4c2a62 to your computer and use it in GitHub Desktop.
Save rafaehlers/506ffd95d7a2b8f4f07fc05a3f4c2a62 to your computer and use it in GitHub Desktop.
Dequeue/Deregister/Remove all scripts of GravityView from the front-end of your website
<?php // don't copy this line
add_action( 'wp_enqueue_scripts', 'gv_dequeue_main_css', 30 );
add_action( 'wp_print_scripts', 'gv_dequeue_main_css', 2 );
function gv_dequeue_main_css() {
wp_dequeue_style( 'gravityview_default_style' );
wp_dequeue_style( 'gravityview_style_default_table' );
wp_dequeue_style( 'gravityview_style_default_list' );
wp_dequeue_style( 'gravityview_style_datatables_table' );
wp_dequeue_style( 'gv-dt_buttons_style' );
wp_dequeue_style( 'gv-dt_fixedheader_style' );
wp_dequeue_style( 'gv-dt_fixedcolumns_style' );
wp_dequeue_style( 'gv-dt_responsive_style' );
wp_dequeue_style( 'gv-dt_scroller_style' );
wp_dequeue_style( 'gv-datatables-featured-entries' );
wp_dequeue_style( 'gravityview_az_entry_filter' );
wp_dequeue_style( 'gravityview-featured-entries' );
wp_dequeue_script( 'gravityview-field-approval' );
wp_dequeue_script( 'gravityview-field-approval-tippy' );
wp_dequeue_script( 'gravityview-field-approval-popper' );
wp_dequeue_style( 'gravityview-field-approval-tippy' );
wp_dequeue_style( 'gravityview_font' );
wp_dequeue_style( 'gravityview_social_wordpress' );
}
Copy link

ghost commented Dec 30, 2019

Prevent from loading if the word 'view' is present in the URL:

if (strpos($_SERVER['REQUEST_URI'], "view") !== false) {
add_action( 'wp_enqueue_scripts', function() {
    $wp_scripts = wp_scripts();
    $wp_styles = wp_styles();
    $scripts = $wp_scripts->registered;
    foreach ( $scripts as &$script ) {
        if ( stripos( $script->src, 'gravityview' ) ) {
            $wp_scripts->dequeue( $script->handle );
        }
    }
    $styles = $wp_styles->registered;
    foreach ( $styles as &$style ) {
        if ( stripos( $style->src, 'gravityview' ) ) {
            $wp_styles->dequeue( $style->handle );
        }
    }
}, 999 );
}

@rafaehlers
Copy link
Author

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