![]() Here’s the initial code from the video, along with my annotations to explain what’s happening. Enqueueing via the functions.php fileĪlthough we recommend enqueuing via creating a plugin, you can still add the commands to your theme’s functions.php file that’s located on the root level of your theme. and in the last parameter you have to add true so it will load in the footer. BX Slider is a great example for demonstrating WP enqueue because it requires a variety of files including jQuery, it’s own JavaScript files and a Stylesheet. Whenever you are adding JS which is depending on jQuery then you can just add third parameter of jquery in wpenqueuescript () function. To demonstrate WP Enqueue in the video above, I installed BX Slider. Rather than hard-coding Scripts and Stylesheets, we can enqueue them into wp_head function via our theme’s functions file. These two very helpful WordPress functions are here to help us do it properly. Files don’t always need to run on every page.Įnter the wp_enqueue_script & wp_enqueue_style functions It is a placeholder to refer to the script you want to insert in the .![]() ![]() ‘script-name’ this value can be anything. WordPress has already done all the hard work for us, we just need to pass in the right parameters to make this work (the two blocks of text within the parentheses). Hard coding scripts in the header mean they’ll run on every page unless you wrap them in conditional statements. wpenqueuescript is a built in WordPress function. Your function in turn instructs WP to add the jQuery script. The above action tells WP to run your function when it is adding the scripts to the html head element. If you’re looking to compress your files via a plugin like WP Minify, then you can’t because they’re hard-coded. addaction ('wpenqueuescripts', 'myscriptsmethod') addaction allows you to run code at specific times during page loads / specific events. Most plugins use jQuery, so chances are you’ll have the script running twice if you hard-code it in. Yes this can work, but it’s not best practice and can have the following side effects: Pasting them directly onto the header.php theme file. Like many front-end developers, I used to add  Scripts and Stylesheets to my themes in a very crude way.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |