If 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:
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.
Solution
To fix this error I needed to add the dependency ‘wp_util’ to the enqueue array as shown on the image below:
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.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- WooCommerce Logout without Confirmation : How to Remove “Are you sure you want to log out?”
- How to Display Category Name in WordPress Using a Practical Example
- How to Display All Products Purchased by User – Purchase History
- How to Get Current User Role in WordPress & Display Roles
- How to Get Current Product Category Name in WooCommerce
- How to Change the WooCommerce ‘Added to Cart’ Notice
- How to Redirect On Refresh WordPress Page » Detect Page Refresh PHP
- How to Add Active Navigation Class Based on URL
- How to Remove Has Been Added to Your Cart Message WooCommerce
- How to Display Error Message in WordPress : Debug WordPress Errors Quickly & Easily
- How to Skip Cart and Redirect to Checkout Page WooCommerce
- How to Redirect a WordPress Page Without Plugins?
- Complete Guide to Using WordPress Custom Hooks do_action & apply_filters With Examples
- Change Proceed To Checkout Text In WooCommerce
- How to Count Items Added to Cart WooCommerce Cart Count Code
Comments are closed.