Here’s the initial code from the video, along with my annotations to explain what’s happening. Don’t worry about being fluent in PHP programming the information you need for this lesson is provided. 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. Enqueueing is used for both scripts and styles and means we must access and alter the functions.php file that comes with your WordPress installation. The correct way to add scripts to WordPress is with wpenqueuescript. 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. Child themes only override php files (like header.php) that are included with functions like gettemplatepart or getheader, etc. 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. However if the assets are located in the Current theme then getstyleshsheetdirectoryuri is used instead. Files don’t always need to run on every page.Įnter the wp_enqueue_script & wp_enqueue_style functions If you creating a Child theme and wanted to access the Parent theme folder and its assets then using gettemplatedirectoryuri is the one to use. And for the login screen, you need to use the loginenqueuescripts action hook, which enqueues custom scripts only for the login page. Hard coding scripts in the header mean they’ll run on every page unless you wrap them in conditional statements. If you want to run your script in the admin area instead of the front-end of your site, you need to use the adminenqueuescripts action hook instead of wpenqueuescripts in the addaction() function. wpenqueuescripts is the proper hook to use when enqueuing scripts and styles that are meant to appear on the front end. Within the hooked function you can use the wpregisterscript (), wpenqueuescript (), wpregisterstyle () and wpenqueuestyle () functions. wpenqueuescripts is the proper hook to use when enqueuing scripts and styles that are meant to appear on the front end. If you’re looking to compress your files via a plugin like WP Minify, then you can’t because they’re hard-coded. To enqueue scripts and styles in the front-end you’ll need to use the wpenqueuescripts hook. Most plugins use jQuery, so chances are you’ll have the script running twice if you hard-code it in. You hook your function into the wpenqueuescripts hook. Here’s how it all comes together: You write a function which registers your scripts using the correct wpenqueue script. Yes this can work, but it’s not best practice and can have the following side effects: The wpenqueue () function is a hook in and of itself, which then hooks into wphead () and wpfooter () as needed. 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 |