WordPress.org

Ready to get started?Download WordPress

Forums

Meteor Slides
Mobile Optimization: avoid loading css and js on every page load (2 posts)

  1. P. W. Erdross
    Member
    Posted 1 year ago #

    Hi, I have seen the "ressource hog" post to late, but using is_frontpage is not a solution for me because my sliders are on several pages.

    I would like to suggest a more flexible solution - you could easily help us with this by adding only four lines of code to your plugin and bring a huge benefit to advanced users:

    in meteor-slides-plugin.php on top add

    defined('METEOR_SLIDES_LOAD_CSS') or define('METEOR_SLIDES_LOAD_CSS', true);
    defined('METEOR_SLIDES_LOAD_JS')  or define('METEOR_SLIDES_LOAD_JS', true);

    then before the call of wp_enqueue_script on line 218 add
    if ( METEOR_SLIDES_LOAD_CSS )

    and before wp_print_scripts on line 244 add
    if ( METEOR_SLIDES_LOAD_JS )

    That´s all!

    This would enable users to add this

    define('METEOR_SLIDES_LOAD_CSS', false);
    define('METEOR_SLIDES_LOAD_JS', false);

    to wp-config.php and then e.g. do this in a template page where the slides are actually used:

    add_action( 'wp_enqueue_scripts', 'css_js_ondemand_meteor' );
    
    function css_js_ondemand_meteor() {
    
            meteorslides_javascript();
            meteorslides_css();
    }

    This can of course be used in other places and ways, however the great effect is that if you know, where your slides are - and usually you do not have them on every page - you can load css and js for your plugin only where it is needed and save some requests on all the other pages!

    Would you mind adding these four lines of code to your plugin?

    I tested it and see no problems - it is just a "hidden gem" for speed-junkies :)

    I adapted this idea from the CF7 contact form plugin author, who finally gave us a solution for not loading the CF7 js + css on every F* page when you have just one contact form... you can check this here:

    http://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/

    Meanwhile there seem to be a few options for doing conditional js and css loading (e.g. [1]), but I think the very quick solution above could help a lot until you feel like figuring out something else.

    [1] http://wordpress.stackexchange.com/questions/20854/conditionally-loading-javascript-css-for-shortcodes

    Thanks for your attention!

    http://wordpress.org/extend/plugins/meteor-slides/

  2. Josh Leuze
    Member
    Plugin Author

    Posted 1 year ago #

    Thanks so much for sharing this tip, I'll try it out and see if I can use it in the plugin!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic