Ajaxify Your WordPress _s (Underscores) Theme With This Code

Ajaxify _s

I’ve created a modified version of the Browserstate (History.js) Ajaxify plugin that works with _s (Underscores). In order for this to work, you’ll need to use wp_enqueue_script() to include the History.js and scrollTo libraries, as well as my modified Ajaxify script. Do this in your existing enqueue function. If you don’t know what that means, check out this article.

wp_enqueue_script( 'scrollto', '//balupton.github.io/jquery-scrollto/lib/jquery-scrollto.js', array( 'jquery' ), '', true );
wp_enqueue_script( 'history-js', '//browserstate.github.io/history.js/scripts/bundled/html4+html5/jquery.history.js', array(), '', true ); 
wp_enqueue_script( 'ajaxify', '//raw.githubusercontent.com/AbacusPowers/ajaxify/master/ajaxify-html5.js', array( 'jquery' ), '', true ); 

Above, you’ll find the lines to include, if you want to load these libraries remotely. I would recommend, however, that you actually take the time to download them, and use a filepath specific to your theme. You can use something like

wp_enqueue_script( 'ajaxify', get_template_directory_uri() . '/js/vendor/ajaxify/ajaxify-html5.js', array( 'jquery' ), '', true );

instead of the direct link to the library on GitHub. To get you started, here’s a direct link to the Ajaxify file download.

Note: The menu update only works for the ‘current-menu-item’ class, at the moment. Ideally, this would be modified to also update any parent or children menu classes as well.

Leave a Reply