litgugl.blogg.se

Wp enqueue
Wp enqueue













wp enqueue

But, remember these functions should be only called after wpenqueuescripts, adminenqueuescripts, or loginenqueuescripts hooks are triggered.

wp enqueue

Here’s how we can enqueue the hashed files automatically! The trick is that we don’t need to know the hash name, we’ll have PHP find the file anyway.įirst we get ready to look through the file system at the directory where our asset is. In WordPress, you have to enqueue scripts and styles with corresponding function wpenqueuescript and wpenqueuestyle.

wp enqueue

pipe(st('build')) // ES6 + Gulp 4Įxport default dev The WordPress Solution If you are using Grunt, there are equivalents. Since I’m using Gulp, gulp-hash came in handy.

#WP ENQUEUE HOW TO#

'/js/', array('jquery'), null, true) Adding the Hashes with Gulpīefore we tackle the WordPress issue, here is how to add hashes. wp_enqueue_script('main', get_template_directory_uri(). However the process for loading scripts is slightly. You will need to make sure to change ‘mypage’ to the slug of your page, ‘script-name’ to whatever you want to use as a unique name of the script, and ‘/path. Simply open your theme’s functions.php file and add the following code at the bottom. You need to manually enqueue the files by name but you can’t know the randomly generated hash value. As you probably know the best way to load scripts in WordPress is to use the wpenqueuescript function. So let’s move on to how to use these tags to conditionally enqueue scripts in WordPress. There is a problem for those of us using WordPress though. This renaming can be done automatically by task runners. For example, if the file `` changes to ``, the browser will definitely re-download it. There is a solution!Īdding a hash in the file name lets the browser think it is a totally different file. I also sometimes notice that when a file was changed, and you refreshed the page, the browser can still be using the old version (because of the cache). It’s a pretty important thing for web page performance. The following is a guest post by Pascal Klau, a self-described trainee web developer from Germany working with WordPress, Gulp, and more recently VueJS and Webpack.ĭuring my journey of learning web technology, I stumbled upon the importance of file caching.















Wp enqueue