Skip to content

Instantly share code, notes, and snippets.

@jessedmatlock
Last active April 26, 2022 02:17
Show Gist options
  • Save jessedmatlock/abb63ec323fa7a8f101a8021198c4605 to your computer and use it in GitHub Desktop.
Save jessedmatlock/abb63ec323fa7a8f101a8021198c4605 to your computer and use it in GitHub Desktop.
GiveWP loads ALL scripts and styles, regardless if a form is present on the page. This script removes them, unless a GiveWP form iframe is present, speeding up your site and reducing load times, etc.
/**
* Deregister and dequeue all GiveWP scripts and styles, unless we're loading them for a GiveWP forms iframe.
**/
function give_deregister_script() {
$deregister = false;
/**
* example gleaned from https://www.boilingpotmedia.com/education/wordpress-conditional-tag-if-url-contains-a-string/
* though that example uses split() [deprecated]
**/
$uri = $_SERVER["REQUEST_URI"]; // get the URL
$uri_array = explode("/",$uri); // explode the URL at each slash to get an array
$uri_first = $uri_array[1]; // select the FIRST part, which is 'give' for iframed forms
if (!$uri_first == 'give'){ // Dequeue GiveWP for ALL pages that don't match the iframe's URL structure
$deregister = true;
}
// Check if the conditions are met to register.
if ( $deregister ) {
wp_deregister_script( 'give' );
wp_dequeue_script( 'give' );
wp_deregister_script( 'give_ffm_frontend' );
wp_dequeue_script( 'give_ffm_frontend' );
wp_deregister_script( 'give_recurring_script' );
wp_dequeue_script( 'give_recurring_script' );
wp_deregister_script( 'give-donation-summary-script-frontend' );
wp_dequeue_script( 'give-donation-summary-script-frontend' );
wp_deregister_script( 'give-stripe-js' );
wp_dequeue_script( 'give-stripe-js' );
wp_deregister_script( 'give-stripe-onpage-js' );
wp_dequeue_script( 'give-stripe-onpage-js' );
wp_deregister_script( 'give-fee-recovery' );
wp_dequeue_script( 'give-fee-recovery' );
wp_deregister_script( 'give-funds-script-frontend' );
wp_dequeue_script( 'give-funds-script-frontend' );
wp_deregister_script( 'give-stripe-payment-request-js' );
wp_dequeue_script( 'give-stripe-payment-request-js' );
wp_deregister_style( 'give-styles' );
wp_dequeue_style( 'give-styles' );
wp_deregister_style( 'give-donation-summary-style-frontend' );
wp_dequeue_style( 'give-donation-summary-style-frontend' );
wp_deregister_style( 'give-funds-style-frontend' );
wp_dequeue_style( 'give-funds-style-frontend' );
wp_deregister_style( 'give-fee-recovery' );
wp_dequeue_style( 'give-fee-recovery' );
wp_deregister_style( 'give_ffm_frontend_styles' );
wp_dequeue_style( 'give_ffm_frontend_styles' );
wp_deregister_style( 'give_ffm_datepicker_styles' );
wp_dequeue_style( 'give_ffm_datepicker_styles' );
wp_deregister_style( 'give_recurring_css' );
wp_dequeue_style( 'give_recurring_css' );
}
}
add_action( 'wp_enqueue_scripts', 'give_deregister_script', 999 );
add_action( 'wp_print_scripts', 'give_deregister_script', 999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment