Home / Admin / WP Simple Pay: Conditionally Dequeue Scripts & Styles
Duplicate Snippet

Embed Snippet on Your Site

WP Simple Pay: Conditionally Dequeue Scripts & Styles

Conditionally dequeue scripts & styles based on set conditions.

In this example, we remove all plugin scripts & styles unless the current page slug is "payment-page", "donate", or "subscribe".

NOT RECOMMENDED: ​Please note that this also prevents Stripe.js from loading on the non-payment form pages. Stripe highly recommends against this. To best leverage Stripe’s advanced fraud functionality, include this script on every page of your site, not just the checkout page. This allows Stripe to detect anomalous behavior that may be indicative of fraud as customers browse your website.

https://stripe.com/docs/stripe-js/reference#including-stripejs

Code Preview
php
<?php
/**
 * @link https://library.wpcode.com/editor/e5wn795d/
 */
add_filter(
	'simpay_before_register_public_scripts',
	/**
	 * @var array<string, string> $scripts
	 * @return array<string, string> $scripts
	 */
	function( $scripts ) {
		// Do not load any scripts unless the user is on one of these pages.
		if ( ! is_page( array( 'payment-page', 'donate', 'subscribe' ) ) ) {
			return array();
		}
		return $scripts;
	},
	20
);
/**
 * @link https://library.wpcode.com/editor/e5wn795d/
 */
add_filter(
	'simpay_before_register_public_styles',
	/**
	 * @var array<string, string> $styles
	 * @return array<string, string> $styles
	 */
	function( $styles ) {
		// Do not load any styles unless the user is on one of these pages.
		if ( ! is_page( array( 'payment-page', 'donate', 'subscribe' ) ) ) {
			return array();
		}
		return $styles;
	},
	20
);

Comments

Add a Comment