WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Performace and Optimization issues (14 posts)

  1. basku
    Member
    Posted 2 years ago #

    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.

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    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.

    http://codex.wordpress.org/Function_Reference/wp_deregister_style
    http://codex.wordpress.org/Function_Reference/wp_deregister_script

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

    Where I deregister stuff for Contact Form 7

  3. basku
    Member
    Posted 2 years ago #

    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.

  4. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    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

    http://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!

  5. basku
    Member
    Posted 2 years ago #

    Thanks a lot again.

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

    Handlers - here i come :)

  6. Marventus
    Member
    Posted 2 years ago #

    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!

  7. basku
    Member
    Posted 2 years ago #

    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.

  8. basku
    Member
    Posted 2 years ago #

    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 :(

  9. Marventus
    Member
    Posted 2 years ago #

    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.

  10. basku
    Member
    Posted 2 years ago #

    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.

  11. Marventus
    Member
    Posted 2 years ago #

    Hi,

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

    Thanks!

  12. basku
    Member
    Posted 2 years ago #

    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.

  13. Marventus
    Member
    Posted 2 years ago #

    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.

  14. basku
    Member
    Posted 2 years ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic