WordPress.org

Ready to get started?Download WordPress

Forums

Simple Real Estate Pack
[resolved] SREP Scripts loading on all pages (11 posts)

  1. Mike
    Member
    Posted 2 years ago #

    When doing speed tests on my site, I see that SREP is loading several scripts on my site:

    /simple-real-estate-pack-4/js/jsmin.js
    /simple-real-estate-pack-4/js/srp.js
    /simple-real-estate-pack-4/js/srp-MortgageCalc.js
    /simple-real-estate-pack-4/lib/jquery.formatCurrency-1.0.0.js

    Plus this image:
    /simple-real-estate-pack-4/images/up_down.gif

    I can appreciate that SREP needs these to do it's job, but why do they have to be loaded on every page on my site, even the ones with no SREP widgets or content? Is there a way to avoid this?

    thanks,
    Mike

    http://wordpress.org/extend/plugins/simple-real-estate-pack-4/

  2. Max Chirkov
    Member
    Plugin Author

    Posted 2 years ago #

    It's easy to apply conditional usage of scripts if they are being used on very specific pages. But since widgets and maps could be used virtually on any post/page, I can't think of a better way to implement this. I'll make a note to myself to look into a better implementation or at least optimization of scripts in the future update.

    Thanks for bringing this up.

  3. Mike
    Member
    Posted 2 years ago #

    OK, thanks. I'm really just using the mortgage calculator on one page.

    Is there an easy way for me to apply conditional usage of scripts by editing SREP somehow? I know how add a script to a single page, but I don't know how to prevent SREP from loading them on all the other pages.

    Thanks again,
    Mike

  4. Max Chirkov
    Member
    Plugin Author

    Posted 2 years ago #

    You can edit srp-function.php file in the /includes/ folder inside of the plugin's folder. Scroll down to function srp_head_scripts() on line 228 and on line 229 type paste this (substitute XX with the numeric ID of your page):

    global $post;
    if('XX' != $post->ID){ return; }

    The next function after (on line 258) srp_foother_scripts() - enter the same code at the beginning of that function as well.

    Basically it will make the scripts load ONLY on that particular page.

  5. Mike
    Member
    Posted 2 years ago #

    Thanks, that worked perfectly! Could I use an array with that code, in case I needed it for another page down the road? Not quite sure to write that...

    thanks again,
    Mike

  6. Max Chirkov
    Member
    Plugin Author

    Posted 2 years ago #

    Sure, with an array the code will look like this:

    global $post;
    $my_posts = array(1, 2, 3);
    if( !in_array($post->ID, $my_posts) ){ return; }

    each item within $my_posts array has to be numeric post ID. All IDs have to be separated by comma.

    P.S. I think I found some information on conditional javascript loading and will try to implement it into my next update. Keep in mind that if you upgrade to the most current version (when it gets released) - all your changes will be overwritten.

  7. Mike
    Member
    Posted 2 years ago #

    OK, great, I'll keep my eyes open for the new release. If it has conditional script loading, I won't worry about it overwriting my changes.

    thanks,
    Mike

  8. Max Chirkov
    Member
    Plugin Author

    Posted 2 years ago #

    Mike, I have made some changes including loading of scripts only on pages that use the plugin. You can download the development version from here and see if it works they way you wanted http://downloads.wordpress.org/plugin/simple-real-estate-pack-4.zip

    Let me know how it works out, if you decide to try it. Thanks!

  9. Mike
    Member
    Posted 2 years ago #

    Yeah, that seems to do the trick! It might also be cool if it only loaded its CSS on the pages where it's needed.

    Thanks again,
    Mike

  10. Max Chirkov
    Member
    Plugin Author

    Posted 2 years ago #

    The problem with any conditional scripts is that you have to have some sort of indicator that plugin is being used on the current page before the HTML rendered on the screen. Shortcodes, for example, can not be detected before page is generated. The header is being generated first, then the content. During content generation shortcode hooks kick-in, so they act during the content generation, but the header is already done. What I did with JS - I moved it all into the footer, this way I still have a chance decide if JS should be printed in the footer or not. CSS on the other hand cannot be injected in the footer. Also, some of the content of my plugin can be loaded and injected into pages via AJAX - this creates another layer of complexity...

    The only way (that I know) to make CSS conditional in my plugin is via JavaScript. Basically not load it into the page at all and only apply after the page is completely loaded via JS.

    On a sidenote, all scripts (including css) should be cached by the user's browser on the first page load, after that the browser will serve cached scripts on all other pages (considering you didn't forbid caching explicitly on the server side).

  11. Mike
    Member
    Posted 2 years ago #

    Gotcha, that makes sense.

    Thanks again!
    Mike

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic