only load plugins on pages that need them

0
308

This code use functions.php file (or custom-functions.php if your theme uses that).

But if you want Contact Form 7 to show up only on your contact page, how do you allow it to do that? By using an if statement, like one of the commentors on Justin’s post suggested:

add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript() {
if ( !is_page('Contact') ) {
wp_deregister_script( 'contact-form-7' );
}
}


other example

add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
if ( !is_page('Events') ) {
wp_deregister_script( 'wpng-calendar' );
wp_deregister_script( 'date-js' );
wp_deregister_script( 'thickbox-js' );
wp_deregister_script( 'jquery-js' );
wp_deregister_script( 'wiky-js' );
}
}

Disable plugins on WordPress for a specific request


/*
Plugin Name: API Load Time Enhancement
Plugin URI: http://subharanjan.com/
Description: Disables plugins for API requests in order to speed up the API response times.
Version: 007
Author: Subh
Author URI: http://subharanjan.com/
*/
$listener_term = '/webservices/';
$current_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '';

// listener for the thin load
if ( strstr( $current_url, $listener_term ) ) {
add_filter( ‘option_active_plugins’, ‘api_request_disable_plugin’ );
}
function api_request_disable_plugin( $plugins ) {
$plugins_not_needed = array(
‘backupwordpress/backupwordpress.php’,
‘wordfence/wordfence.php’,
‘contact-form-7-to-database-extension/contact-form-7-db.php’,
‘contact-form-7/wp-contact-form-7.php’,
‘wp-piwik/wp-piwik.php’,
‘simple-responsive-slider/simple-responsive-slider.php’,
‘google-sitemap-plugin/google-sitemap-plugin.php’,
‘category-page-icons/menu-compouser.php’,
‘easy-fancybox/easy-fancybox.php’,
‘business-owner-switch/business-owner-switch.php’,
‘wordpress-seo/wp-seo.php’
);

foreach ( $plugins_not_needed as $plugin ) {
$key = array_search( $plugin, $plugins );
if ( false !== $key ) {
unset( $plugins[ $key ] );
}
}

return $plugins;
}