Remove Plugin scripts from loading in other plugin pages

Home Forums BulletProof Security Pro Remove Plugin scripts from loading in other plugin pages

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #12151
    AITpro Admin
    Keymaster

    UPDATE BPS Pro 14+ and BPS free 3.5+ versions:  The Script|Style Loader Filter (SLF) In BPS Plugin Pages option is now turned On by default.  Previously there was a performance problem caused by turning SLF On, but that issue has been figured out and now SLF increases BPS plugin page performance significantly.

    UPDATE: Current versions of BPS/BPS Pro have an option setting under the Setup menu > UI|UX menu > Script|Style Loader Filter (SLF) In BPS Plugin Pages option > Turn On SLF, that uses/already has this code below in BPS.

    Note: The BPS Pro SLF filter may or may work in every single case and may actually cause worse problems, but it is worth a try to see if it works or not – see the help info below.

    Read Me help button text on the UI|UX page:
    Script|Style Loader Filter (SLF) In BPS Plugin Pages:
    SLF is set to Off by default. If BPS plugin pages are not displaying visually correct then select the Turn On SLF option and click the Save Option button. This option prevents other plugin and theme scripts, which break BPS plugin pages visually, from loading in BPS plugin pages. In some cases turning the SLF option On can cause an even worse problem, which is that BPS plugin pages load extremely slowly. So if you turn On the SLF option and BPS plugin pages are displaying visually correct, but BPS plugin pages are loading extremely slowly then unfortunately turning Off SLF is the lesser of the two problems – BPS plugin pages will not display visually correct.
    _________________________________________________________________________________
    This is older information below: See the newer information above.

    This function will remove all rogue plugin & theme scripts and styles from loading in your plugin pages and breaking stuff in your plugin pages.

    Change the preg_match condition to your particular plugin’s Request URI/plugin page path. Example: page=your-plugin-page-path.  And of course change the function name by adding your plugin’s initials/identifier:  xyz_kill_rogue_plugin_theme_scripts_styles.  Or if the plugin is breaking a lot of other plugins and wreaking havoc all throughout the WP Dashboard area then remove the preg_match condition or add the other plugin page paths.  Removing the preg_match condition altogether means that the scripts and styles for that plugin or theme will not load anywhere in the WP Dashboard period including the rogue plugin or theme pages.

    Add your particular plugin’s js/script handle and any other jquery scripts that your plugin uses to the script_handles array.  The BPS plugin uses several jquery handles that your particular plugin may not be using so only add your jquery handles.  Both the script_handles array and the style_handles array contain standard/default WordPress handles that need to be included otherwise you will block those, which will cause problems.  Be sure to leave the standard/default WordPress script and style handles in the arrays.

    add_action( 'admin_enqueue_scripts', 'example_register_enqueue_scripts_styles' );
    
    // Register scripts and styles, Enqueue scripts and styles, Dequeue any plugin or theme scripts and styles loading in BPS plugin pages
    function example_register_enqueue_scripts_styles() {
    global $wp_scripts, $wp_styles, $bulletproof_security, $wp_version;
    
    	// Register & Load BPS scripts and styles on BPS plugin pages ONLY
    	if ( preg_match( '/page=bulletproof-security/', esc_html($_SERVER['REQUEST_URI']), $matches) ) {
    
    		$UIoptions = get_option('bulletproof_security_options_theme_skin');
    
    		// Register BPS Scripts
    		wp_register_script( 'bps-tabs', plugins_url( '/bulletproof-security/admin/js/bps-ui-tabs.js' ) );
    		wp_register_script( 'bps-dialog', plugins_url( '/bulletproof-security/admin/js/bps-ui-dialog.js' ) );	
    		wp_register_script( 'bps-accordion', plugins_url( '/bulletproof-security/admin/js/bps-ui-accordion.js' ) );
    	
    		// Register BPS Styles
    		if ( version_compare( $wp_version, '3.8', '>=' ) ) {
    		
    			switch ( $UIoptions['bps_ui_theme_skin'] ) {
        			case 'blue':
    					wp_register_style('bps-css-38', plugins_url('/bulletproof-security/admin/css/bps-blue-ui-theme.css'));
    				break;
        			case 'grey':
    					wp_register_style('bps-css-38', plugins_url('/bulletproof-security/admin/css/bps-grey-ui-theme.css'));
    				break;
        			case 'black':
    					wp_register_style('bps-css-38', plugins_url('/bulletproof-security/admin/css/bps-black-ui-theme.css'));
    				break;
    			default: 		
    					wp_register_style('bps-css-38', plugins_url('/bulletproof-security/admin/css/bps-blue-ui-theme.css'));		
    			}
    		
    		} else {
    		
    			wp_register_style('bps-css', plugins_url('/bulletproof-security/admin/css/bps-blue-ui-theme-old-wp-versions.css'));
    		}
    
    		// Enqueue BPS scripts & script dependencies
    		wp_enqueue_script( 'jquery-ui-tabs', plugins_url( '/bulletproof-security/admin/js/bps-ui-tabs.js' ), array( 'jquery' ) );
    		wp_enqueue_script( 'jquery-ui-dialog', plugins_url( '/bulletproof-security/admin/js/bps-ui-dialog.js' ), array( 'jquery' ) );
    		wp_enqueue_script( 'jquery-effects-blind', plugins_url( '/bulletproof-security/admin/js/bps-ui-dialog.js' ), array( 'jquery-effects-core' ) );		
    		wp_enqueue_script( 'jquery-effects-explode', plugins_url( '/bulletproof-security/admin/js/bps-ui-dialog.js' ), array( 'jquery-effects-core' ) );	
    		wp_enqueue_script( 'jquery-ui-accordion', plugins_url( '/bulletproof-security/admin/js/bps-ui-accordion.js' ), array( 'jquery' ) );
    		wp_enqueue_script( 'bps-tabs' );
    		wp_enqueue_script( 'bps-dialog' );
    		wp_enqueue_script( 'bps-accordion' );	
    
    		// Enqueue BPS stylesheets
    		if ( version_compare( $wp_version, '3.8', '>=' ) ) {
    		
    			switch ( $UIoptions['bps_ui_theme_skin'] ) {
        			case 'blue':
    					wp_enqueue_style('bps-css-38', plugins_url('/bulletproof-security/admin/css/bps-blue-ui-theme.css'));
    				break;
        			case 'grey':
    					wp_enqueue_style('bps-css-38', plugins_url('/bulletproof-security/admin/css/bps-grey-ui-theme.css'));;
    				break;
        			case 'black':
    					wp_enqueue_style('bps-css-38', plugins_url('/bulletproof-security/admin/css/bps-black-ui-theme.css'));
    				break;
    			default: 		
    					wp_enqueue_style('bps-css-38', plugins_url('/bulletproof-security/admin/css/bps-blue-ui-theme.css'));		
    			}
    		
    		} else {
    		
    			wp_enqueue_style('bps-css', plugins_url('/bulletproof-security/admin/css/bps-blue-ui-theme-old-wp-versions.css'));
    		}	
    
    		// Dequeue any other plugin or theme scripts that should not be loading on BPS plugin pages
    		$script_handles = array( 'bps-tabs', 'bps-dialog', 'bps-accordion', 'admin-bar', 'jquery', 'jquery-ui-core', 'jquery-ui-tabs', 'jquery-ui-dialog', 'jquery-ui-accordion', 'jquery-effects-core', 'jquery-effects-blind', 'jquery-effects-explode', 'common', 'utils', 'svg-painter', 'wp-auth-check', 'debug-bar' );
    
    		$style_handles = array( 'bps-css', 'bps-css-38', 'admin-bar', 'colors', 'ie', 'wp-auth-check', 'debug-bar' );
    
    		foreach( $wp_scripts->queue as $handle ) {
    		
    			if ( ! in_array( $handle, $script_handles ) ) {
    				wp_dequeue_script( $handle );
            		// uncomment line below to see all the script handles that are being blocked on BPS plugin pages
    				//echo 'Script Dequeued: ' . $handle . ' | ';
    			}
    		}
    	
    		foreach( $wp_styles->queue as $handle ) {
            	
    			if ( ! in_array( $handle, $style_handles ) ) {
    				wp_dequeue_style( $handle );
    				// uncomment line below to see all the style handles that are being blocked on BPS plugin pages
    				//echo 'Style Dequeued: ' . $handle . ' | ';
    			}	
    		}
    	}
    }
    

    CAUTION:  These additional filters will stop Rogue scripts that cannot be dequeued since they are not being enqueued appropriately, but may cause severe plugin page load problems.  If you are going to use this code then be sure to provide an On|Off DB option setting.
    Example Results: nulls/kills the Rogue scripts by adding -script-nulled to the script path:
    /plugins/piklist/parts/css/jquery-ui/jquery-ui.piklist.css-script-nulled
    /plugins/nextend-facebook-connect/buttons/facebook-btn.css-script-nulled
    /plugins/appointments/css/admin.css-script-nulled
    /plugins/easy-media-gallery/includes/css/admin.css-script-nulled

    $bpsPro_SLF_options = get_option('bulletproof_security_options_SLF');
    
    if ( $bpsPro_SLF_options['bps_slf_filter'] == 'On' ) {
    
    	if ( is_admin() && preg_match( '/page=bulletproof-security/', esc_html($_SERVER['REQUEST_URI']), $matches ) ) {
    
    	add_filter( 'style_loader_tag', 'example_style_loader_filter' );
    	add_filter( 'script_loader_tag', 'example_script_loader_filter' );
    
    	}
    }
    
    // Prevents other plugin and theme Styles from loading in BPS plugin pages
    function example_style_loader_filter($tag){
    	
    	if ( preg_match( '/page=bulletproof-security/', esc_html($_SERVER['REQUEST_URI']), $matches) ) {
    
    		if ( ! strpos( $tag, 'bulletproof-security' ) && ! strpos( $tag, 'wp-admin' ) && ! strpos( $tag, 'wp-includes' ) )
    
    			$tag = preg_replace( '/\.css/', ".css-script-nulled", $tag );
    	
    		return $tag;
    	}
    }
    
    // Prevents other plugin and theme Scripts from loading in BPS plugin pages
    function example_script_loader_filter($tag){
    
    	if ( preg_match( '/page=bulletproof-security/', esc_html($_SERVER['REQUEST_URI']), $matches) ) {
    
    		if ( ! strpos( $tag, 'bulletproof-security' ) && ! strpos( $tag, 'wp-admin' ) && ! strpos( $tag, 'wp-includes' ) )
    
    			$tag = preg_replace( '/\.js/', ".js-script-nulled", $tag );
    
    		return $tag;
    	}
    }

    Important Note: If you are using BPS 404 error logging code or other 404 error logging code in your theme’s 404.php template then 404 errors will be logged due to nulling/killing rogue scripts in BPS plugin pages. To prevent these 404 errors from being logged, a new condition has been added to the BPS 404.php template file code (BPS Pro 10.3/10.4 and BPS .52). The new code condition and instructions for how to use the BPS /wp-content/plugins/bulletproof-security/404.php template file logging code are included in the BPS 404.php template file.  if ( ! preg_match( '/page=bulletproof-security/', esc_html($_SERVER['HTTP_REFERER']), $matches) ) {.

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.