Algolia - WordPress - Uncaught TypeError: wp.template is not a function

1.4k Views Asked by At

I am getting this error: Uncaught TypeError: wp.template is not a function on template for Algolia. It is clearly associated with the widget that calls the hits:

search.addWidget(
              instantsearch.widgets.hits({
                container: '#hits',
                hitsPerPage: 30,
                templates: {
                item: wp.template('instantsearch-hit'),
                empty: "We didn't find any results for the search <em>\"{{query}}\"</em>"
                }
                })
                );

From what I already know this needs wp-util.js script to work (this is supposed to be part of WordPress and this script can be found: /wp-includes/js/wp-util.js

More about it here: https://codex.wordpress.org/Javascript_Reference/wp.template

It seems the issue is that the template is not loading for some reason. I tried to enqueue the script via functions.php, but after doing so apart from getting previous error I get two new errors:

Uncaught ReferenceError: _ is not defined at wp-util.js:17 at wp-util.js:124

Did anyone had such issue and managed to resolve it already?

Thank you for your help.

3

There are 3 best solutions below

0
On

Are you using Cloudflare rocket loader by any chance?

There is a bug report for the same error that is apparently caused by Cloudflare rocket loader.

The solution mentioned in that bug report is:

I applied a small workaround to prevent the js error, which was blocking other scripts in my website, adding:

if( wp.template == null ) return;

right after the firing of the script:

jQuery(function () {

In this way, my following scripts are fired correctly, but sometimes (let's say 1 time every 4 page loads) the autocomplete search is not fired.

0
On

In my case I had W3 Total Cache plugin installed, and Minify option was set to Manual.

After I removed wp-underscore script from manual minify list, issue with the wp.template() Not a function was gone.

0
On

Faced same issue in my wp's admin panel. And found that "BackUpWordpress" plugin was the problem. I just deactivate it and checked page again. Error was gone.

So the solution is just deactivate additional plugins one by one and check errored page every time. This is how you can have the idea which plugin is responsible for this jquery error.

I confirmed that to deactivate plugin will not erase saved data inside that plugins. So there is no issue to deactivate plugin. Once your work complete on the page where "Uncaught TypeError: wp.template is not a function" error occurred, you can activate plugins again.