Posted on

SLP Maps and Late Loading Scripts

Since the Store Locator Plus 3.2 release, our SLP plugin now defaults to the less efficient “Early Loading” mechanism for rendering our required JavaScript.  This adds a lot of unnecessary overhead for other pages on the site but works with more themes.

Pro Pack users have an option to turn off “early loading” with the “force load javascript” checkbox on the main settings page.   Turn this off if your theme and plugins are “late loading friendly”.  Your site will just a little faster on all the non-SLP pages.

The Discussion

We often run into sites that do not load the Store Locator Plus map ever since our version 3.X update that uses late loading to be more intelligent about how scripts are loaded.   Here is an excerpt from an investigation with a client installation related to this issue that will shed some light on the problem.


 I need to create an option for “early loading” and “late loading” of scripts.  There are simply too many themes or plugins that break late-loading plugins.

 Late loading is WAAAAYYYY more efficient.   What it means is we can detect “hey, this page is showing a SLP map” and only then load all the heavy javascript to support it (google maps API, our scripts, etc.).
Early loading means we load all of our scripts on every single page, regardless of whether the page needs it or not.  The bonus… it always works.    The downside, it is slower.  Especially on very busy sites.
However, in the past 6 months since we changed to the smarter model we have come to realize there are THOUSANDS of plugins or themes that are just not well engineered.   Thus we realize we must engineer our plugin to be even smarter.
With version 3.2 Store Locator Plus will default to “early loading” and always load our scripts.  It will work on more sites but be slower.    The Pro Pack will have an option to turn on “late loading”, removing the early loading and making the plugin more efficient.  If it doesn’t work (like on your site) you simply uncheck the box and go back to the slow but working method.
In the long run you are better off using only themes and plugins that do not break plugins that take advantage of the late loading feature available since WordPress 3.2.  Your site will load faster as more & more plugins start using this methodology.
The short version, it is going to take quite a bit more effort but this is the best solution.   The fix for your site is to select a theme & plugins that are well engineered.  Your new site is not working because the theme is not calling the proper WordPress 3.2+ action hook sequence.
The bottom line of all this.  Some plugins and themes are not well engineered.  When we release version 3.2 of SLP we will provide an option to “be slow and work around bad plugins/themes” or “be fast, I am using a great theme and well designed plugins”.


After days of looking for workarounds to the problem we have discovered that a key element for Store Locator Plus to function efficiently is the use of wp_localize_script.  This is the cleanest and most efficient way to get PHP variables, like your map settings, into the JavaScript engine for Google Maps.
Some themes, it turns out, do not properly support late loading scripts.  In order for wp_localize_script to work after a shortcode has been rendered the script must be loaded in the footer, or “late loaded”.
We do this to ensure that any attributes you add to the shortcode are passed to the JavaScript engine.  This is the only way to override map defaults on a per-page basis.   Thus, if your theme does not support late loading scripts (which is a WordPress 3.x+ standard) then Store Locator Plus may not work on your site.
The first thing to do is ask your theme provider to update their theme to properly support late loading and localized scripts.   Then check back here for updates.  We are working hard to make our plugin compatible across as many themes and plugins as possible.

Update #2

A follow-on discussion with our client about why we do late loading and why “other plugins don’t seem to break but SLP does”.
Most plugins will not have a problem because they are not nearly as complex as Store Locator Plus.
We use 3 very inter-dependent elements of WordPress and JavaScript to make the plugin as efficient as possible and not slow down your site.   All 3 pieces need to fire at the right time and the process cannot be interrupted.
1) Register the script early in the WordPress process.
2) Localize the script (very few plugins do this, less than 0.1%).  This is the best method for getting WordPress settings into JavaScript.   It is relatively new, very few developers know about it.  As such almost no plugins do this.  It is sad because WordPress promotes this as THE WAY to get JavaScript & WordPress talking efficiently.
3) Enqueue the script.
99.9% of plugins will never break because they don’t localize scripts.  Instead they do the old inefficient (and insecure I might add) way of getting WordPress data into JavaScript.    Sadly since 99.9% of plugins that need WordPress data in JavaScript just copy what someone else did, instead of learning how to do it properly, the problem gets replicated more than TWO YEARS after WordPress provided a better solution.
I know because I did the same thing 2 years ago until I discovered localization of scripts and spent time learning how it works and why it is better.
So the short version… it does not surprise me that other plugins don’t break when someone (Media Grid) inadvertently short-circuits late loading JavaScript like that found in SLP.
The checkbox, BTW, turns OFF late loading JavaScript. SLP does not break but every page load is going to be slightly slower on your site as long as the box is checked.
For “fun” you should look at a random page and see all the CSS and JavaScript that loads.   80% of it is never needed/used for the page you are looking at.   That is because 99.99% of plugins don’t care they just force their stuff to load ALL THE TIME whether it is needed or not (early loading).   We explicitly try to NOT do that mostly because we need to talk to Google.  Run your site on a slow Internet connection and you’ll see why we try to only load up our stuff when absolutely necessary.