How to remove JS & CSS bloat that is loaded on every page?
One notorious example would be plugin: Contact Form 7
Official document: Loading JavaScript and Stylesheet Only When it is Necessary
Od pluginova, tu je strategio/cf7unloaded koji se bavi sa shortcode-ovima a ne sa block, kao i Scripts Removal for Contact Form 7 koji
Remove Contact Form 7 scripts and styles through the WordPress action hook API
Conditionally Loading Scripts and Styles for WordPress Plugins
By looking at source code, has_shortcode() functions seems quite slow, so I will use more simpler approach.
function wpdocs_shortcode_scripts() {
global $post;
if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'wpdocs-shortcode') ) {
wp_enqueue_script( 'wpdocs-script');
}
}
add_action( 'wp_enqueue_scripts', 'wpdocs_shortcode_scripts', 999);
--
$page_id = get_queried_object_id();
$page_object = get_page( $page_id );
if ( strpos($page_object->post_content, '[/slider]')
--
JS: wp_register_script( 'jquery-form'
JS: wp_enqueue_script( 'contact-form-7'
CSS: wp_enqueue_style( 'contact-form-7'
CSS: wp_enqueue_style( 'contact-form-7-rtl'
do_action( 'wpcf7_enqueue_scripts' );
do_action( 'wpcf7_enqueue_styles' );
--
# old way of removing scripts
add_action( 'wpcf7_enqueue_styles', function() { wp_deregister_style( 'contact-form-7' ); } );
add_action( 'wpcf7_enqueue_scripts', function() { wp_deregister_script( 'jquery-form' ); } );
--
@see: http://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
wpcf7_enqueue_scripts();
}
if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
wpcf7_enqueue_styles();
}
Plugin: Disable plugins on conditions
Plugin list
-
Plugin Logic with great overview article at Plugin Logic - A simple, but forgotten performance plugin - David Waumsley. Due to rarrely updated it can’t uninstall properly by itself.
-
Plugin Load Filter with explanation at article Using Plugin Load Filter has a strange UI but works exactly as needed
-
Swift Performance Lite includes great plugin organizer only in Paid version
Ugly UX
- Plugin Organizer really awful UI
- Freesoul Deactivate Plugins
- Plugin Disabler
- Asset CleanUp: Page Speed Booster has nice UI but really works only in Pro version
- WordPress Assets manager, dequeue scripts, dequeue styles is now part of Clearfy
Paid plugins
- Gonzales was the first WordPress plugin that can disable unneeded scripts on a page-level basis.
- Paid Deactivate Plugins Per Page is paid but best UI by far
- Paid WP Plugin Manager Pro, free version: WP Plugin Manager
Abandoned
- WordPress › Script Logic is not working anymore
I can always do the same programmatically:
For scripts?
Conditionally Loading CSS for Shortcodes Conditional Script Loading Revisited
Odličan tekst o optimizaciji sa primenom na CF7 optimize WesztyWeb - Script Loading Optimizations
Load on need:
https://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/
Using an HTML5 Button Element as Contact Form 7 Submit - WPKlik
Najzad!
Contact Form 7 5.7.5 | Contact Form 7
Block editor: Introduces wpcf7_enqueue_block_editor_assets() and retrieves contact forms only when needed.
Sve najbolje sam primenio na:
cnc / cf7-conditionaly-load-assets.php
contact-form-7-conditional-enqueues.php je dobar conditional load za CF7 u smislu da se dva puta na enqueueuje kada ima više formi na istoj strani
-
To disable enqueueing
gtm4wp-form-move-tracker.js
script UncheckEvents » Form fill events
that is an option send event when a visitor moves between elements of a form (comment, contact, etc). -
To disable enqueueing
gtm4wp-contact-form-7-tracker.js
script UncheckIntegration » Contact Form 7 » Contact Form 7
that is an option to fire events after Contact Form 7 submissions.