• Resolved basku

    (@bha100710)


    Hello !

    Three wp-plugins – ‘cforms’, ‘paypal shopping cart’ and ‘simple forum’ are addding a large chunk of javascript and css files to the header of my website.

    Technically they should be populating the header only when they are required.

    The problem is, they load on all posts – irrespective of wether the functionality of that plugin is required or not on that post.

    For e.g the shopping cart JS and CSS should load only if the product/ check out pages are opened. Similary the forum scripts should only load – when the forum is assesed.

    Needless to say, this anamoly is affecting my sites overall performace .

    I guess many users like me must be getting affected by this unwanted intrusion.

    Is there a hack or possible solution to override the loading of unnecessary js and css on all pages ??

    Thanks in advance.

Viewing 13 replies - 1 through 13 (of 13 total)
  • cforms has a setting to only load their stuff on certain pages.

    But in general, from your functions.php, you can write up functions to check for certain pages, and stop the scripts and styles from loading on all others.

    https://codex.www.ads-software.com/Function_Reference/wp_deregister_style
    https://codex.www.ads-software.com/Function_Reference/wp_deregister_script

    And you can see the concept here:
    https://vudu.me/4o

    Where I deregister stuff for Contact Form 7

    Thread Starter basku

    (@bha100710)

    Thanks Rev. Voodoo for pointing me that direction. I read your post and got some idea. Learnt a new thing.

    I am not a coder by profession – just been playing around with them. Please help me out with the coding.

    Suppose I want to allow a JS
    ” wp-content/plugins/simple-forum/resources/jscript/forum/sf-forum.js?ver=5635″
    to load only if url points to pages starting with /forum, how do i go about doing that.

    I copied your cform code and modified it as follows –

    add_action( 'wp_print_scripts', 'xyz_deregister_javascript', 100 );
    function xyz_deregister_javascript() {
    if ( !is_page('forum') ) {
    wp_deregister_script( 'sf-forum.js?ver=5635' );
        }
    }

    I am not very comfortable especially with the parameters that need to be specified with the wp_deregister_script.

    Thanks again.

    I think you are close…

    the ?ver=5635 stuff is versioning, you wouldn’t use that…

    But actually, you don’t deregister the script by file name, you deregister it by handle

    https://justintadlock.com/archives/2009/08/06/how-to-disable-scripts-and-styles
    Maybe that can help you better than I can. Unfortunately, you have to dig a bit to find exactly how to deregister stuff

    I’m also not a coder…definitely not! Just have strong google-fu!

    Thread Starter basku

    (@bha100710)

    Thanks a lot again.

    time to dig into each of the plugins to look for those intrusive handlers.

    Handlers – here i come ??

    Hi there,

    you might find this article by Justin Tadlock pertinent to your issue. I read it recently while I was optimizing style and script loading on a site, and it was very helpful.

    Cheers!

    Thread Starter basku

    (@bha100710)

    Thanks Marventus/ Voodo – went through the suggested articles.

    And while i think i have followed things said over there, i am still facing issues. The same old js and css files appear in header, even after going through the suggested process.

    Here’s what I did:

    STEP 1: I located the handlers for the plugin in question – “paypal shopping cart”

    add_action('wp_head', 'wp_cart_css');
    
    add_action('wp_head', 'wp_cart_add_read_form_javascript');

    The said plugin does not use

    wp_enqueue_style

    Accordingly the handlers are:

    for css – wp_cart_css
    for js –wp_cart_add_read_form_javascript

    I then used the following code in functions.php. I used an if statement with condition !is_page() because, i needed this plugin to load only on pages and not on single post or archive pages.

    /* ------------------------------------------------------------------------------------
    Code to Deregister unwanted plugin related styles & scripts from loading on all unncecessary pages
    ----------------------------------------------------------------------------------------*/
    
    //Deregister scripts
    add_action( 'wp_print_scripts', 'bhaskar_deregister_javascript', 100 );
    //DEREGISTER wp cart javascript on all posts - load only if is page
    function bhaskar_deregister_javascript() {
    	if ( !is_page() ) {
    		wp_deregister_script( 'wp_cart_add_read_form_javascript' );
    	}
    }
    
    //Deregister styles
    //DEREGISTER wp cart css on all posts - load only if is page
    add_action( 'wp_print_styles', 'bhaskar_deregister_styles', 100 );
    function bhaskar_deregister_styles() {
    	if ( !is_page()  ) {
    	wp_deregister_style( 'wp_cart_css' );
    	}
    }

    Even after this action – the culprit js file and the wp_shopping_cart_style.css still load onto the header in every post and archive page.

    What could i be missing – Any ideas ?

    Thanks for the help.

    Thread Starter basku

    (@bha100710)

    Will you advice trying this alternative ?

    In the cart plugin i came across the code:

    function wp_cart_css()
    
    {
    
        echo '<link type="text/css" rel="stylesheet" href="'.WP_CART_URL.'/wp_shopping_cart_style.css" />'."\n";
    
    }

    Will Replacing it with this code get it right ?

    if ( is_page()  ) {    echo '<link type="text/css" rel="stylesheet" href="'.WP_CART_URL.'/wp_shopping_cart_style.css" />'."\n";
    	}
    else { <?php } ?>

    But this is not a update proof hack, i guess ??

    Unfortunately, it appears that the method in which the wp_cart_css style sheet is added leaves you no choice but to edit the plugin’s core code to load the styles conditionally.
    However, if you want to have more flexibility over when and how to call the style sheet and if you also wish to do things the right way, you should leave that line of code unchanged and you simply comment it out:

    // echo '<link type="text/css" rel="stylesheet" href="'.WP_CART_URL.'/wp_shopping_cart_style.css" />'."\n";

    Then, you can use wp_enqueue_style from your functions.php file to load the styles conditionally.
    I would also advice you to contact the plugin author and to suggest him to use wp_enqueue_style in his next release. That way, if he does, you would be able to dequeue the style globally and then call it only when needed all from functions.php, without modifying the plugin files at all.

    Thread Starter basku

    (@bha100710)

    thanks Marventus.. I will go with your idea of commenting out the line and including this small css in the main style.css

    thanks again for the support.

    Marventus

    (@marventus)

    Hi,

    Where you able to figure this out? If so, could you please mark your topic as resolved?

    Thanks!

    Thread Starter basku

    (@bha100710)

    Well I could figure out the process but unfortunately the plugin does not use wp_enqueue_style.

    I have already marked this as resolved. Thanks again.

    Marventus

    (@marventus)

    Sorry, I was going over the latest topics I contributed to, and didn’t see the resolved tag.
    As for the style sheet, didn’t commenting out the line in question not work?
    Once the style sheet is prevented from loading by default, you can either incorporate the styles to your style.css, or better yet, enqueue it conditionally from your functions.php as it should have been in the plugin itself.

    Thread Starter basku

    (@bha100710)

    Commenting out the line did the work..but it is not an update proof solution.

    If the plugin developer would have used wp_enqueue_style, I could have a permanent solution by adding a custom function.

    Anyways..i never knew about the existence of core function wp_deregister_script.. so defintely a value addition into my arsenal for fututre use ??

    Thanks for your replies.

    Will definetely cross roads again ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Performace and Optimization issues’ is closed to new replies.