How to deregister js_composer css and javascript file on single posts?
I try this code but it doesn’t work
add_action( 'wp_print_styles', 'aa_deregister_styles', 100 );
function aa_deregister_styles() {
if ( ! is_page( 'contact' ) ) {
wp_deregister_style( 'contact-form-7' );
}
if ( ! is_front_page() ) {
wp_deregister_style( 'js_composer' );
wp_dequeue_style( 'js_composer' );
}
}
add_action( 'wp_print_scripts', 'aa_deregister_javascript', 100 );
function aa_deregister_javascript() {
if ( ! is_page( 'contact' ) ) {
wp_deregister_script( 'contact-form-7' );
}
if ( ! is_front_page() ) {
wp_deregister_script( 'js_composer' );
wp_dequeue_script( 'js_composer' );
}
}
The code work with contact-form-7 but not work with js_composer
Some javascript and css files of Visual composer is big, but i only need it on Home page, not all page
-
This topic was modified 10 years by
gvdk.
I just use this in the functions.php
Just change the 111 and 222 to whatever page ID’s you have contact forms on. I have two so there are two page ID’s. I have tested on google page speed test and contact form doesn’t load script or css other than the two pages I want it to.
# Restrict Contact form 7 scripts and styles
function conditionally_load_contactform7(){
if(! is_page( array ( 111 , 222 ) ) ) {
wp_dequeue_script('contact-form-7');
wp_dequeue_style('contact-form-7');
}
}
add_action( 'wp_enqueue_scripts', 'conditionally_load_contactform7' );
Hi,
You can try to check for pagebuilder so if you do not use pagebuilder on a page, the css and js will be removed:
add_action( 'wp_print_styles', 'aa_deregister_styles', 10 );
function aa_deregister_styles()
{
$td_use_page_builder = false;
if (method_exists('WPBMap', 'getShortCodes')) {
$td_use_page_builder = true;
}
if ($td_use_page_builder == true) {
wp_deregister_style('js_composer_front');
wp_deregister_script('js_composer_front');
}
}
Hope this helps.
Thanks.
Thank you for your help, now it work for me and my site is loading faster very much.
Total size of my webpage is under 3 MB now, include adsense.
I had added this code to functions.php (desktop)
But is there bug if i add this code to functions.php for mobile theme?
-
This reply was modified 10 years by
gvdk.
Hi,
I have not tested this on the mobile theme, but the mobile theme does not use visual composer at all so this method is redundant: https://forum.tagdiv.com/the-mobile-theme/
Thanks.
Thank you for your help but i still saw js_composer javascript and css files when i view source code mobile site or when i check with google speed insights.
You can see here:
Hello gvdk,
Please notice that the theme still uses the Visual Composer-style for the mobile theme because this is required. Even though the mobile theme cannot be customized using the Visual Composer, it uses the elements from this when displaying the Big Grid from the top of the page and the Latest Articles module from the bottom of the page and these cannot be got rid.
Thank you for your understanding!