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, 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, 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.

Useful links:


    1. Yeah, the admin bar doesn’t work quite right. I’ll find a way to exclude requests to /wp-admin/ from Turbolinks in an update. Or you can make a pull request on GitHub!

  1. This article was really useful, Thank you!

    I did “cry” for days trying to identify the best pjax approach for my wp site in order to make it loading pages via ajax.
    I tried many ways and approaches (plugins, libraries, templates etc…) but nothing was really working. Pjaxy was the more productive but I did not came up with preparring the pjax templates coming from woocommerce plugin.

    This plugin is simply amazing, because it does what you expect without needing any special setting.. Just installed and activated.

    A great job. Thank you!

  2. You are amazing. one feature if this plugin have it will be the best plugin ever:
    loading bar, it’s the only thing this plugin needs. it’s will be cool if there was the top screen loading line like youtube.com have

Leave a Reply

Your email address will not be published. Required fields are marked *