How to Fix Uncaught TypeError: wp.template is not a function

Error FixedIf you come across this error Uncaught TypeError: wp.template is not a function on the console you should not panic since I have a quick one minute solution for this error. I came across the Uncaught TypeError: wp.template is not a function error in my recent development of WooCommerce redirect after checkout plugin.

Fix Uncaught TypeError: wp.template is Not a function

In this plugin, I used the I used the wp.template template interpolation to quickly generate the blocks of views on the fly as you can see on the image below:

Conditional Redirect Options

The problem was I need to generate the conditional views and allow the user to pick what was needed in his case.

The plugin worked well until a customer support ticket that raised an issue that the add button was not working. I quickly asked for the logins and checked the console.

Console Uncaught TypeError: wp.template is not a function

When I checked the console every time I clicked the add button, the block template was not generated and the console had this error repeated every time I clicked the button.

When I investigated it further I saw the error was caused by the unavailability of the wp.template() function and immediately knew this was as a result of a bad enqueue of the dependency or possible some jQuery conflict. 

Uncaught TypeError: wp.template is not a function

Solution

To fix this error I needed to add the dependency ‘wp_util’ to the enqueue array as shown on the image below:

Uncaught TypeError: wp.template is not a function

When you enqueue admin script do not forget to reference the ‘wp-util’ in the array and as in the code below. This is the cause of the error and you can fix it by adding the reference to your enqueue script code as in the sample code below :

wp_enqueue_script( 
          'script_handle',
           plugin_dir_url( __FILE__ ) . 'script.js',
           array( 'jquery', 'wp-util' ), // Always add the dependencies example 'wp-util'
          '1.0', 
           true 
          );

I hope this solution helps you to fix this error.

Similar Articles

  1. How to Count Items Added to Cart WooCommerce Cart Count Code

 

Comments are closed.