Loading pages with AJAX in WordPress

Turbolinks script has been around for quite a while in the Ruby on Rails community, but never before has it been so easy to implement it anywhere else.

In the past, I’ve made themes with PJAX, but I found it quite limiting, so I quickly rewrote using Pronto.js, which has worked out fine (for example in the Bluebird Theme), but it also had its limits.

One of the most prominent issues with loading pages with AJAX is the lack of support for dynamic, page to page scripts and styles. So for example, if a widget on a page is pulled through AJAX using the Pronto.js (or PJAX) method – the script and the styles for that widget aren’t pulled in. So you get just the blank HTML. This is a huge issue when making a WordPress theme because you just can’t prepare the theme for everything the user might throw at the theme.

Turbolinks to the rescue

In comparison – Turbolinks is pure magic. It pulls in the whole HTML, parses it, compares to your current page. Any additional styles or scripts are pulled in dynamically. In some scenarios, this still might not work flawlessly, but the success rate is significantly higher than with other libraries.

It pulls in the whole HTML, parses it, compares to your current page, and if any additional styles or scripts are required – it pulls them in dynamically. In some scenarios, this still might not work flawlessly, but the success rate is significantly higher than with other libraries.

I like Turbolinks so much, that I decided to package it into a WordPress plugin – so here it is – Turbolinks for WordPress

The beauty of Turbolinks is that it requires no setup. Install & Activate the plugin and you’re done. Check your site – if it works – great, if it doesn’t no harm done, just deactivate the plugin and that’s it.

There is an added benefit packing PJAX functionality in a WordPress plugin. When building themes, I can just include it as a recommended plugin in a theme. If something isn’t quite compatible with Turbolinks – the user is able to just disable the plugin. No tweaks, no options. Simple and clean!

This is simply the best solution for loading pages through AJAX in WordPress.
On top of everything else – there is an Events API for theme and plugin authors. You can read more about that in the Turbolinks GitHub page.