{"id":10806,"date":"2023-04-07T08:35:34","date_gmt":"2023-04-07T07:35:34","guid":{"rendered":"https:\/\/www.20i.com\/blog\/?p=10806"},"modified":"2026-01-08T10:16:16","modified_gmt":"2026-01-08T10:16:16","slug":"wordpress-enqueue-introduction","status":"publish","type":"post","link":"https:\/\/www.20i.com\/blog\/wordpress-enqueue-introduction\/","title":{"rendered":"WordPress Enqueue Script and How To Use It\u00a0"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">WordPress is a favourite website builder for many because of its ease of use and customisability.\u202f&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In particular, Cascading Style Sheets (CSS) and JavaScript power the content management system\u2019s large library of themes and <a href=\"https:\/\/www.20i.com\/blog\/essential-wordpress-plugins\/\" target=\"_blank\" rel=\"noreferrer noopener\">plugins<\/a> that help bring people\u2019s websites to life.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But did you know there\u2019s a right and wrong way to load these styles and scripts? The correct way is with something called WordPress enqueue script.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But don\u2019t let the name intimidate you. It\u2019s very easy to implement it correctly.\u202f&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you\u2019ll learn about what enqueue script is, how to use it, and why it\u2019s critical for your WordPress site.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is WordPress enqueue script?&nbsp;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An enqueue script is a command that adds JavaScript files and CSS style sheets to WordPress functions, allowing you to use them whenever you need to without having to rewrite code.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of adding the custom scripts or style sheets directly to your WordPress header file, you insert a snippet of code that informs WordPress which assets to add. WordPress then automatically links those assets in the header and footer.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why is enqueueing important?&nbsp;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can think of enqueueing as a way of getting WordPress to add styles and scripts for you instead of doing it manually. But, besides automation, why is enqueueing important?&nbsp;<\/p>\n\n\n\n<ul start=\"1\" class=\"wp-block-list\">\n<li><strong>It prevents errors. <\/strong>WordPress is an open-source project with thousands of developers worldwide creating themes and plugins. By providing a system of loading scripts and style sheets, enqueueing helps prevent conflicts between these themes and plugins.&nbsp;<\/li>\n\n\n\n<li><strong>It improves site performance. <\/strong>A mistake many new WordPress developers make is adding JavaScript or CSS directly into their plugins or themes, which could lead to wasted resources.\u202f&nbsp;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if you manually add <a href=\"https:\/\/www.w3schools.com\/jquery\/jquery_intro.asp\" target=\"_blank\" rel=\"noreferrer noopener\">jQuery scripts<\/a> to your WordPress website and install a plugin that loads jQuery, your website ends up loading jQuery twice. All that duplicate code, if left unchecked, could end up <a href=\"https:\/\/www.20i.com\/blog\/website-speed-testing\/\" target=\"_blank\" rel=\"noreferrer noopener\">reducing page load time<\/a>.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to enqueue WordPress scripts&nbsp;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Enqueueing involves two steps: registering the script (or style sheet) and enqueueing it.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Two steps are required due to the modular nature of scripts. In other words, you want WordPress to store the new asset as you may not want to use it all the time.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The extra step also allows other WordPress site owners to deregister your script \u2014 i.e., <em>only <\/em>include the shortcode on the required pages \u2014 without having to modify your plugin&#8217;s core code.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s walk through the two steps:&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1. Registering the script&nbsp;<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The rationale behind registering your script is to let WordPress know it exists.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the wp_register_script() or wp_register_styles() function, depending on what you want to enqueue.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The sample code looks like this:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wp_enqueue_script( string $handle, string $src = '',  \nstring&#091;] $deps = array(), string|bool|null $ver = false,  \nbool $in_footer = false ) <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The code has five parameters:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$handle<\/strong> refers to the unique name of your script. (e.g., \u201cmy_script\u201d)&nbsp;\n<ul class=\"wp-block-list\">\n<li>Type of parameter: string&nbsp;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$src<\/strong> is the full URL of your script or the path of the script relative to the WordPress root directory.&nbsp;\n<ul class=\"wp-block-list\">\n<li>Type of parameter: string&nbsp;<\/li>\n\n\n\n<li>Default value: \u2018 \u2018&nbsp;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$deps <\/strong>refers to dependency. It is an array of registered script handles the script depends on.&nbsp; For example, if your script uses jQuery, jQuery will be your $deps value and WordPress will automatically load jQuery if your site doesn\u2019t load it already.&nbsp;\n<ul class=\"wp-block-list\">\n<li>Type of parameter: string[ ]&nbsp;<\/li>\n\n\n\n<li>Default value: array( )&nbsp;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$ver<\/strong> is an optional parameter that specifies the script\u2019s version number. If there is one, it is added to the URL as a query for cache busting. If none exists, the version number is set to the current WordPress version.\u00a0\n<ul class=\"wp-block-list\">\n<li>Type of parameter: string|bool|null&nbsp;<\/li>\n\n\n\n<li>Default value: false&nbsp;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$in_footer<\/strong> is an optional parameter that lets you choose whether to load the script in the footer or header. Set the value to <strong>true <\/strong>if you want to load it in the footer and to <strong>false <\/strong>if you want to load it in the header.&nbsp;\n<ul class=\"wp-block-list\">\n<li>Type of parameter: bool&nbsp;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note: <\/strong>It\u2019s better to enqueue scripts at the footer since it improves site load time. The more scripts and styles in your website\u2019s header, the longer it takes for the browser to go through them.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Enqueueing the script&nbsp;<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Once you\u2019ve registered the parameters script and styles, it\u2019s time to use WordPress enqueue script. The command tells WordPress to \u201cadd on\u201d \u2014 enqueue \u2014 a new JavaScript file.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use wp_enqueue_script for both scripts and styles, but you can also use these scripts within the hooked function:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>wp_register_script()&nbsp;<\/li>\n\n\n\n<li>wp_enqueue_script()&nbsp;<\/li>\n\n\n\n<li>wp_register_style()&nbsp;<\/li>\n\n\n\n<li>wp_enqueue_style()\u202f&nbsp;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Once you\u2019ve input the code, run the <a href=\"https:\/\/learn.wordpress.org\/tutorial\/wordpress-action-hooks\/\" target=\"_blank\" rel=\"noreferrer noopener\">action hook<\/a> (add_action () ) to load the script.\u202f&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then, add the code to the WordPress plugin file or your theme\u2019s functions.php file.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sample codes&nbsp;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sample 1: Enqueueing the function in two steps&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'wp_enqueue_scripts', 'custom_plugin_assets' ); \nfunction custom_plugin_assets() { \n \nwp_register_style('20i_script', plugins_url( '\/css\/portfolio.css' , __FILE__ ));\nwp_register_script('20i_script', plugins_url( '\/js\/portfolio.js' , __FILE__ )); \n \nwp_enqueue_style ('20i_script'); \nwp_enqueue_script('20i_script'); \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Sample 2: Enqueueing the function in one step&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'wp_enqueue_scripts', 'custom_plugin_assets' ); \nfunction custom_plugin_assets() { \nwp_enqueue_style( '20i_script', plugins_url( '\/css\/portfolio.css' , __FILE__ ) ); \nwp_enqueue_script( '20i_script', plugins_url( '\/js\/portfolio.js' , __FILE__ ) ); \n} <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">I understand WordPress enqueue script \u2014 what next?&nbsp;<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As we\u2019ve seen in this guide, script enqueueing allows you to customise your WordPress page assets in a simple but powerful way.\u202f&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition, this tool offers your website a number of benefits, like reducing duplicate code and improving page load times.\u202f&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But to take full advantage of WordPress enqueue script, you should be using a <a href=\"https:\/\/www.20i.com\/web-hosting\" data-internallinksmanager029f6b8e52c=\"16\" title=\"web hosting\">web hosting<\/a> service specifically designed for WordPress.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Contact us today to find a <a href=\"https:\/\/www.20i.com\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\">Managed WordPress Hosting<\/a> solution that\u2019s right for you.<\/p>\n\n\n<div class='code-block code-block-4' style='margin: 8px 0; clear: both;'>\n<hr>\n<br \/><a href=\"https:\/\/www.20i.com\/wordpress-hosting\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/www.20i.com\/blog\/wp-content\/uploads\/2026\/03\/Blog-Ad-WordPress-1200x625-1.png\" loading=\"lazy\" alt=\"Managed WordPress Hosting\"><\/a><\/div>\n\n\n\n\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"http:\/\/schema.org\",\n  \"@type\": \"Webpage\",\n  \"name\": \"Managed WordPress Hosting\",\n  \"url\": \"https:\/\/www.20i.com\/wordpress-hosting\",\n  \"@id\": \"https:\/\/www.20i.com\/wordpress-hosting#webpage\"\n \n}\n<\/script>\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"http:\/\/schema.org\",\n  \"@type\": \"WebPage\",\n  \"@id\": \"https:\/\/www.20i.com\/blog\/wordpress-enqueue-introduction\/#webpage\",\n  \"url\": \"https:\/\/www.20i.com\/blog\/wordpress-enqueue-introduction\/\",\n  \"inLanguage\": \"en-gb\",\n  \"name\": \"An Introduction to WordPress Enqueue Script and How To Use It\",\n  \"description\": \"Learn about what enqueue script is, how to use it, and why it\u2019s critical for your WordPress site. \",\n  \"isPartOf\": {\n    \"@id\": \"https:\/\/www.20i.com\/wordpress-hosting#webpage\"\n  },\n  \"about\": [\n    {\n      \"@type\": \"Thing\",\n      \"name\": \"enqueue script\",\n      \"sameAs\": \"https:\/\/en.wikipedia.org\/wiki\/Queue_(abstract_data_type)\"\n    },\n{\n      \"@type\": \"Thing\",\n      \"name\": \"WordPress\",\n      \"sameAs\": \"https:\/\/en.wikipedia.org\/wiki\/WordPress\"\n    }\n  ],\n  \"mentions\": [\n    {\n      \"@type\": \"Thing\",\n      \"name\": \"CSS\",\n      \"sameAs\": \"https:\/\/en.wikipedia.org\/wiki\/CSS\"\n    },\n    {\n      \"@type\": \"Thing\",\n      \"name\": \"Java Script\",\n      \"sameAs\": \"https:\/\/en.wikipedia.org\/wiki\/JavaScript\"\n    },\n    {\n      \"@type\": \"Thing\",\n      \"name\": \"plugins\",\n      \"sameAs\": \"https:\/\/en.wikipedia.org\/wiki\/WordPress#Plugins\"\n    },\n    {\n      \"@type\": \"Thing\",\n      \"name\": \"themes\",\n      \"sameAs\": \"https:\/\/en.wikipedia.org\/wiki\/WordPress#Themes\"\n    },\n    {\n      \"@type\": \"Thing\",\n      \"name\": \"website\",\n      \"sameAs\": \"https:\/\/en.wikipedia.org\/wiki\/Website\"\n    }\n  ]\n}\n<\/script>\n<script type=\"application\/ld+json\">\n{ \n\"@context\": \"http:\/\/schema.org\", \n\"@type\": \"FAQPage\", \"@id\": \"https:\/\/www.20i.com\/blog\/wordpress-enqueue-introduction\/#FAQ\",\n\"url\": \"https:\/\/www.20i.com\/blog\/wordpress-enqueue-introduction\/\", \n\"headline\": \"An Introduction to WordPress Enqueue Script and How To Use It \", \n\"isPartOf\":{\"@id\":\"https:\/\/www.20i.com\/blog\/wordpress-enqueue-introduction\/#webpage \"},\n\"inLanguage\":\"en-gb\",\n      \"mainEntity\": [\n   {\n       \"@type\": \"Question\",\n    \"name\": \"What is WordPress enqueue script? \",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"<p> An enqueue script is a command that adds JavaScript files and CSS style sheets to WordPress functions, allowing you to use them whenever you need to without having to rewrite code.  <\/p>\n<p> Instead of adding the custom scripts or style sheets directly to your WordPress header file, you insert a snippet of code that informs WordPress which assets to add. WordPress then automatically links those assets in the header and footer.  <\/p>\"\n    }\n  },\n{\n       \"@type\": \"Question\",\n    \"name\": \"Why is enqueueing important?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"<p>You can think of enqueueing as a way of getting WordPress to add styles and scripts for you instead of doing it manually. But, besides automation, why is enqueueing important? <\/p>\n<ol><li>It prevents errors. WordPress is an open-source project with thousands of developers worldwide creating themes and plugins. By providing a system of loading scripts and style sheets, enqueueing helps prevent conflicts between these themes and plugins. <\/li>\n<li>It improves site performance. A mistake many new WordPress developers make is adding JavaScript or CSS directly into their plugins or themes, which could lead to wasted resources.\u202f <\/li><\/ol>\n<p>For example, if you manually add jQuery scripts to your WordPress website and install a plugin that loads jQuery, your website ends up loading jQuery twice. All that duplicate code, if left unchecked, could end up reducing page load time. <\/p>\"\n    }\n  }\n] \n}\n<\/script>\n","protected":false},"excerpt":{"rendered":"WordPress is a favourite website builder for many because of its ease of use and customisability.\u202f&nbsp; In particular,&hellip;","protected":false},"author":13,"featured_media":8956,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"ub_ctt_via":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"csco_singular_sidebar":"","csco_page_header_type":"","csco_page_load_nextpost":"","footnotes":""},"categories":[60],"tags":[79,76],"class_list":["post-10806","post","type-post","status-publish","format-standard","has-post-thumbnail","category-web-design-dev","tag-web-development","tag-wordpress","cs-entry"],"featured_image_src":"https:\/\/www.20i.com\/blog\/wp-content\/uploads\/2022\/07\/WP-blocks.jpg","author_info":{"display_name":"Matthew Telfer","author_link":"https:\/\/www.20i.com\/blog\/author\/matthew-telfer\/"},"_links":{"self":[{"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/posts\/10806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/comments?post=10806"}],"version-history":[{"count":8,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/posts\/10806\/revisions"}],"predecessor-version":[{"id":18153,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/posts\/10806\/revisions\/18153"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/media\/8956"}],"wp:attachment":[{"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/media?parent=10806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/categories?post=10806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.20i.com\/blog\/wp-json\/wp\/v2\/tags?post=10806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}