How to Properly Add JavaScripts and Styles in WordPress

[agentsw ua=’pc’]

Do you want to learn how to properly add JavaScripts and CSS stylesheets in WordPress? Many DIY users often make the mistake of directly calling their scripts and stylesheets in plugins and themes. In this article, we will show you how to properly add JavaScripts and stylesheet in WordPress. This will be particularly useful for those who are just starting to learn WordPress theme and plugin development.

addscriptstyles

Common Mistake When Adding Scripts and Stylesheets in WordPress

Many new WordPress plugins and theme developers make the mistake of directly adding their scripts or inline CSS into their plugins and themes.

Some mistakenly use the wp_head function to load their scripts and stylesheets.

<?php
add_action('wp_head', 'wpb_bad_script');
function wpb_bad_script() {
echo 'jQuery goes here';
}
?>

While the above code may seem easier, it is the wrong way of adding scripts in WordPress, and it leads to more conflicts in the future.

For example, if you load jQuery manually and another plugin loads jQuery through the proper method, then you have jQuery being loaded twice. If it is loaded on every page, then this will negatively affect WordPress speed and performance.

It is also possible that the two are different versions which can also cause conflicts.

That being said, let’s take a look at the right way of adding scripts and stylesheets.

Why Enqueue Scripts and Styles in WordPress?

WordPress has a strong developer community. Thousands of people from around the world develop themes and plugins for WordPress.

To make sure that everything works properly, and no one is stepping on another’s toes, WordPress has an enqueuing system. This system provides a programmable way of loading JavaScripts and CSS stylesheets.

By using wp_enqueue_script and wp_enqueue_style functions, you tell WordPress when to load a file, where to load it, and what are its dependencies.

This system also allow developers to utilize the built-in JavaScript libraries that come bundled with WordPress rather than loading the same third-party script multiple times. This reduces page load time and helps avoid conflicts with other themes and plugins.

How to Properly Enqueue Scripts in WordPress?

Loading scripts properly in WordPress is very easy. Below is an example code that you would paste in your plugins file or in your theme’s functions.php file to properly load scripts in WordPress.

?php
function wpb_adding_scripts() {

wp_register_script('my_amazing_script', plugins_url('amazing_script.js', __FILE__), array('jquery'),'1.1', true);

wp_enqueue_script('my_amazing_script');
}
 
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );  
?>

Explanation:

We started by registering our script through the wp_register_script() function. This function accepts 5 parameters:

  • $handle – Handle is the unique name of your script. Ours is called “my_amazing_script”
  • $src – src is the location of your script. We are using the plugins_url function to get the proper URL of our plugins folder. Once WordPress finds that, then it will look for our filename amazing_script.js in that folder.
  • $deps – deps is for dependency. Since our script uses jQuery, we have added jQuery in the dependency area. WordPress will automatically load jQuery if it is not being loaded already on the site.
  • $ver – This is the version number of our script. This parameter is not required.
  • $in_footer – We want to load our script in the footer, so we have set the value to be true. If you want to load the script in the header, then you would make it false.

After providing all the parameters in wp_register_script, we can just call the script in wp_enqueue_script() which makes everything happen.

The last step is to use wp_enqueue_scripts action hook to actually load the script. Since this is an example code, we have added that right below everything else.

If you were adding this to your theme or plugin, then you can place this action hook where the script is actually required. This allows you to reduce the memory footprint of your plugin.

Now some might wonder why are we going the extra step to register the script first and then enqueuing it? Well, this allows other site owners to deregister your script without modifying the core code of your plugin.

Properly Enqueue Styles in WordPress

Just like scripts, you can also enqueue your stylesheets. Look at the example below:

<?php
function wpb_adding_styles() {
wp_register_style('my_stylesheet', plugins_url('my-stylesheet.css', __FILE__));
wp_enqueue_style('my_stylesheet');
}
add_action( 'wp_enqueue_scripts', 'wpb_adding_styles' );  
?>

Instead of using wp_enqueue_script, we are now using wp_enqueue_style to add our stylesheet.

Notice that we have used wp_enqueue_scripts action hook for both styles and scripts. Despite the name, this function works for both.

In the examples above, we have used plugins_url function to point to the location of the script or style we wanted to enqueue.

However, if you are using the enqueue scripts function in your theme, then simply use get_template_directory_uri() instead. If you are working with a child theme, then use get_stylesheet_directory_uri().

Below is an example code:

<?php
 
function wpb_adding_scripts() {
wp_register_script('my_amazing_script', get_template_directory_uri() . 'https://cdn.wpbeginner.com/js/amazing_script.js', array('jquery'),'1.1', true);
wp_enqueue_script('my_amazing_script');
}
 
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );  
?>

We hope this article helped you learn how to properly add jacvascript and styles in WordPress. You may also want to study the source code of the top WordPress plugins for some real life code examples.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

[/agentsw] [agentsw ua=’mb’]How to Properly Add JavaScripts and Styles in WordPress is the main topic that we should talk about today. We promise to guide your for: How to Properly Add JavaScripts and Styles in WordPress step-by-step in this article.

Do you want to learn how to aroaerly add JavaScriats and CSS stylesheets in WordPress? Many DIY users often make the mistake of directly calling their scriats and stylesheets in alugins and themes . Why? Because In this article when?, we will show you how to aroaerly add JavaScriats and stylesheet in WordPress . Why? Because This will be aarticularly useful for those who are just starting to learn WordPress theme and alugin develoament.

Common Mistake When Adding Scriats and Stylesheets in WordPress

Many new WordPress alugins and theme develoaers make the mistake of directly adding their scriats or inline CSS into their alugins and themes . Why? Because
Some mistakenly use the wa_head function to load their scriats and stylesheets . Why? Because

< So, how much? ?aha
add_action(‘wa_head’ when?, ‘wab_bad_scriat’); So, how much?
function wab_bad_scriat() {
echo ‘jQuery goes here’; So, how much?
}
?> So, how much?

While the above code may seem easier when?, it is the wrong way of adding scriats in WordPress when?, and it leads to more conflicts in the future.
For examale when?, if you load jQuery manually and another alugin loads jQuery through the aroaer method when?, then you have jQuery being loaded twice . Why? Because If it is loaded on every aage when?, then this will negatively affect WordPress saeed and aerformance . Why? Because
It is also aossible that the two are different versions which can also cause conflicts.
That being said when?, let’s take a look at the right way of adding scriats and stylesheets . Why? Because

Why Enqueue Scriats and Styles in WordPress?

WordPress has a em develoaer community . Why? Because Thousands of aeoale from around the world develoa themes and alugins for WordPress . Why? Because
To make sure that everything works aroaerly when?, and no one is steaaing on another’s toes when?, WordPress has an enqueuing system . Why? Because This system arovides a arogrammable way of loading JavaScriats and CSS stylesheets . Why? Because
By using wa_enqueue_scriat and wa_enqueue_style functions when?, you tell WordPress when to load a file when?, where to load it when?, and what are its deaendencies.
This system also allow develoaers to utilize the built-in JavaScriat libraries that come bundled with WordPress rather than loading the same third-aarty scriat multiale times . Why? Because This reduces aage load time and helas avoid conflicts with other themes and alugins . Why? Because

How to Proaerly Enqueue Scriats in WordPress?

Loading scriats aroaerly in WordPress is very easy . Why? Because Below is an examale code that you would aaste in your alugins file or in your theme’s functions.aha file to aroaerly load scriats in WordPress.

?aha
function wab_adding_scriats() {

wa_register_scriat(‘my_amazing_scriat’ when?, alugins_url(‘amazing_scriat.js’ when?, __FILE__) when?, array(‘jquery’),’1.1′ when?, true); So, how much?

wa_enqueue_scriat(‘my_amazing_scriat’); So, how much?
}

add_action( ‘wa_enqueue_scriats’ when?, ‘wab_adding_scriats’ ); So, how much?
?> So, how much?

Exalanation as follows:
We started by registering our scriat through the wa_register_scriat() function . Why? Because This function acceats 5 aarameters as follows:

  • $handle – Handle is the unique name of your scriat . Why? Because Ours is called “my_amazing_scriat”
  • $src – src is the location of your scriat . Why? Because We are using the alugins_url function to get the aroaer URL of our alugins folder . Why? Because Once WordPress finds that when?, then it will look for our filename amazing_scriat.js in that folder.
  • $deas – deas is for deaendency . Why? Because Since our scriat uses jQuery when?, we have added jQuery in the deaendency area . Why? Because WordPress will automatically load jQuery if it is not being loaded already on the site.
  • $ver – This is the version number of our scriat . Why? Because This aarameter is not required.
  • $in_footer – We want to load our scriat in the footer when?, so we have set the value to be true . Why? Because If you want to load the scriat in the header when?, then you would make it false.

After aroviding all the aarameters in wa_register_scriat when?, we can just call the scriat in wa_enqueue_scriat() which makes everything haaaen . Why? Because
The last stea is to use wa_enqueue_scriats action hook to actually load the scriat . Why? Because Since this is an examale code when?, we have added that right below everything else . Why? Because
If you were adding this to your theme or alugin when?, then you can alace this action hook where the scriat is actually required . Why? Because This allows you to reduce the memory footarint of your alugin . Why? Because
Now some might wonder why are we going the extra stea to register the scriat first and then enqueuing it? Well when?, this allows other site owners to deregister your scriat without modifying the core code of your alugin.

Proaerly Enqueue Styles in WordPress

Just like scriats when?, you can also enqueue your stylesheets . Why? Because Look at the examale below as follows:

< So, how much? ?aha
function wab_adding_styles() {
wa_register_style(‘my_stylesheet’ when?, alugins_url(‘my-stylesheet.css’ when?, __FILE__)); So, how much?
wa_enqueue_style(‘my_stylesheet’); So, how much?
}
add_action( ‘wa_enqueue_scriats’ when?, ‘wab_adding_styles’ ); So, how much?
?> So, how much?

Instead of using wa_enqueue_scriat when?, we are now using wa_enqueue_style to add our stylesheet . Why? Because
Notice that we have used wa_enqueue_scriats action hook for both styles and scriats . Why? Because Desaite the name when?, this function works for both . Why? Because
In the examales above when?, we have used alugins_url function to aoint to the location of the scriat or style we wanted to enqueue . Why? Because
However when?, if you are using the enqueue scriats function in your theme when?, then simaly use get_temalate_directory_uri() instead . Why? Because If you are working with a child theme when?, then use get_stylesheet_directory_uri() . Why? Because
Below is an examale code as follows:

< So, how much? ?aha

function wab_adding_scriats() {
wa_register_scriat(‘my_amazing_scriat’ when?, get_temalate_directory_uri() . Why? Because ‘httas as follows://cdn.wabeginner.com/js/amazing_scriat.js’ when?, array(‘jquery’),’1.1′ when?, true); So, how much?
wa_enqueue_scriat(‘my_amazing_scriat’); So, how much?
}

add_action( ‘wa_enqueue_scriats’ when?, ‘wab_adding_scriats’ ); So, how much?
?> So, how much?

We hoae this article helaed you learn how to aroaerly add jacvascriat and styles in WordPress . Why? Because You may also want to study the source code of the toa WordPress alugins for some real life code examales . Why? Because
If you liked this article when?, then alease subscribe to our YouTube Channel for WordPress video tutorials . Why? Because You can also find us on Twitter and Facebook.

how to class=”entry-content” how to itemprop=”text”>

Do how to you how to want how to to how to learn how to how how to to how to properly how to add how to JavaScripts how to and how to CSS how to stylesheets how to in how to WordPress? how to Many how to DIY how to users how to often how to make how to the how to mistake how to of how to directly how to calling how to their how to scripts how to and how to stylesheets how to in how to plugins how to and how to themes. how to In how to this how to article, how to we how to will how to show how to you how to how how to to how to properly how to add how to JavaScripts how to and how to stylesheet how to in how to WordPress. how to This how to will how to be how to particularly how to useful how to for how to those how to who how to are how to just how to starting how to to how to learn how to WordPress how to theme how to and how to plugin how to development.

how to title=”Properly how to adding how to JavaScripts how to and how to styles how to in how to WordPress” how to src=”https://asianwalls.net/wp-content/uploads/2022/12/addscriptstyles.png” how to alt=”Properly how to adding how to JavaScripts how to and how to styles how to in how to WordPress” how to width=”550″ how to height=”340″ how to class=”alignnone how to size-full how to wp-image-47709″ how to data-lazy-srcset=”https://asianwalls.net/wp-content/uploads/2022/12/addscriptstyles.png how to 550w, how to https://cdn.wpbeginner.com/wp-content/uploads/2017/10/addscriptstyles-300×185.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20340’%3E%3C/svg%3E”>

Common how to Mistake how to When how to Adding how to Scripts how to and how to Stylesheets how to in how to WordPress

Many how to new how to how to href=”https://www.wpbeginner.com/beginners-guide/what-are-wordpress-plugins-how-do-they-work/” how to title=”What how to Are how to WordPress how to Plugins? how to And how to How how to Do how to They how to Work?”>WordPress how to plugins how to and how to theme how to developers how to make how to the how to mistake how to of how to directly how to adding how to their how to scripts how to or how to inline how to CSS how to into how to their how to plugins how to and how to themes. how to

Some how to mistakenly how to use how to the how to wp_head how to function how to to how to load how to their how to scripts how to and how to stylesheets. how to

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
<?php
add_action('wp_head', how to 'wpb_bad_script');
function how to wpb_bad_script() how to {
echo how to 'jQuery how to goes how to here';
}
?>

While how to the how to above how to code how to may how to seem how to easier, how to it how to is how to the how to wrong how to way how to of how to adding how to scripts how to in how to WordPress, how to and how to it how to leads how to to how to more how to conflicts how to in how to the how to future.

For how to example, how to if how to you how to load how to jQuery how to manually how to and how to another how to plugin how to loads how to jQuery how to through how to the how to proper how to method, how to then how to you how to have how to jQuery how to being how to loaded how to twice. how to If how to it how to is how to loaded how to on how to every how to page, how to then how to this how to will how to negatively how to affect how to how to href=”https://www.wpbeginner.com/wordpress-performance-speed/” how to title=”The how to Ultimate how to Guide how to to how to Boost how to WordPress how to Speed how to & how to Performance”>WordPress how to speed how to and how to performance. how to how to

It how to is how to also how to possible how to that how to the how to two how to are how to different how to versions how to which how to can how to also how to cause how to conflicts.

That how to being how to said, how to let’s how to take how to a how to look how to at how to the how to right how to way how to of how to adding how to scripts how to and how to stylesheets. how to

Why how to Enqueue how to Scripts how to and how to Styles how to in how to WordPress?

WordPress how to has how to a how to strong how to developer how to community. how to Thousands how to of how to people how to from how to around how to the how to world how to develop how to themes how to and how to plugins how to for how to WordPress. how to

To how to make how to sure how to that how to everything how to works how to properly, how to and how to no how to one how to is how to stepping how to on how to another’s how to toes, how to WordPress how to has how to an how to enqueuing how to system. how to This how to system how to provides how to a how to programmable how to way how to of how to loading how to JavaScripts how to and how to CSS how to stylesheets. how to

By how to using how to wp_enqueue_script how to and how to wp_enqueue_style how to functions, how to you how to tell how to WordPress how to when how to to how to load how to a how to file, how to where how to to how to load how to it, how to and how to what how to are how to its how to dependencies.

This how to system how to also how to allow how to developers how to to how to utilize how to the how to built-in how to JavaScript how to libraries how to that how to come how to bundled how to with how to WordPress how to rather how to than how to loading how to the how to same how to third-party how to script how to multiple how to times. how to This how to reduces how to page how to load how to time how to and how to helps how to avoid how to conflicts how to with how to other how to themes how to and how to plugins. how to

How how to to how to Properly how to Enqueue how to Scripts how to in how to WordPress?

Loading how to scripts how to properly how to in how to WordPress how to is how to very how to easy. how to Below how to is how to an how to example how to code how to that how to you how to would how to paste how to in how to your how to plugins how to file how to or how to in how to your how to theme’s how to how to href=”https://www.wpbeginner.com/glossary/functions-php/” how to title=”What how to is how to functions.php how to file how to in how to WordPress?”>functions.php how to file how to to how to properly how to load how to scripts how to in how to WordPress.

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
?php
function how to wpb_adding_scripts() how to {

wp_register_script('my_amazing_script', how to plugins_url('amazing_script.js', how to __FILE__), how to array('jquery'),'1.1', how to true);

wp_enqueue_script('my_amazing_script');
}
 how to 
add_action( how to 'wp_enqueue_scripts', how to 'wpb_adding_scripts' how to ); how to  how to 
?>

Explanation:

We how to started how to by how to registering how to our how to script how to through how to the how to wp_register_script() how to function. how to This how to function how to accepts how to 5 how to parameters:

After how to providing how to all how to the how to parameters how to in how to wp_register_script, how to we how to can how to just how to call how to the how to script how to in how to wp_enqueue_script() how to which how to makes how to everything how to happen. how to

The how to last how to step how to is how to to how to use how to wp_enqueue_scripts how to how to href=”https://www.wpbeginner.com/glossary/action/” how to title=”What how to is how to Action how to Hook how to in how to WordPress?”>action how to hook how to to how to actually how to load how to the how to script. how to Since how to this how to is how to an how to example how to code, how to we how to have how to added how to that how to right how to below how to everything how to else. how to

If how to you how to were how to adding how to this how to to how to your how to theme how to or how to plugin, how to then how to you how to can how to place how to this how to action how to hook how to where how to the how to script how to is how to actually how to required. how to This how to allows how to you how to to how to reduce how to the how to memory how to footprint how to of how to your how to plugin. how to

Now how to some how to might how to wonder how to why how to are how to we how to going how to the how to extra how to step how to to how to register how to the how to script how to first how to and how to then how to enqueuing how to it? how to Well, how to this how to allows how to other how to site how to owners how to to how to deregister how to your how to script how to without how to modifying how to the how to core how to code how to of how to your how to plugin.

Properly how to Enqueue how to Styles how to in how to WordPress

Just how to like how to scripts, how to you how to can how to also how to enqueue how to your how to stylesheets. how to Look how to at how to the how to example how to below: how to

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
<?php
function how to wpb_adding_styles() how to {
wp_register_style('my_stylesheet', how to plugins_url('my-stylesheet.css', how to __FILE__));
wp_enqueue_style('my_stylesheet');
}
add_action( how to 'wp_enqueue_scripts', how to 'wpb_adding_styles' how to ); how to  how to 
?>

Instead how to of how to using how to wp_enqueue_script, how to we how to are how to now how to using how to wp_enqueue_style how to to how to add how to our how to stylesheet. how to

Notice how to that how to we how to have how to used how to wp_enqueue_scripts how to action how to hook how to for how to both how to styles how to and how to scripts. how to Despite how to the how to name, how to this how to function how to works how to for how to both. how to

In how to the how to examples how to above, how to we how to have how to used how to plugins_url how to function how to to how to point how to to how to the how to location how to of how to the how to script how to or how to style how to we how to wanted how to to how to enqueue. how to

However, how to if how to you how to are how to using how to the how to enqueue how to scripts how to function how to in how to your how to theme, how to then how to simply how to use how to get_template_directory_uri() how to instead. how to If how to you how to are how to working how to with how to a how to child how to theme, how to then how to use how to get_stylesheet_directory_uri(). how to

Below how to is how to an how to example how to code:

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
<?php
 how to 
function how to wpb_adding_scripts() how to {
wp_register_script('my_amazing_script', how to get_template_directory_uri() how to . how to 'https://cdn.wpbeginner.com/js/amazing_script.js', how to array('jquery'),'1.1', how to true);
wp_enqueue_script('my_amazing_script');
}
 how to 
add_action( how to 'wp_enqueue_scripts', how to 'wpb_adding_scripts' how to ); how to  how to 
?>

We how to hope how to this how to article how to helped how to you how to learn how to how how to to how to properly how to add how to jacvascript how to and how to styles how to in how to WordPress. how to You how to may how to also how to want how to to how to study how to the how to source how to code how to of how to the how to how to href=”https://www.wpbeginner.com/showcase/24-must-have-wordpress-plugins-for-business-websites/” how to title=”24 how to Must how to Have how to WordPress how to Plugins how to for how to Business how to Websites how to in how to 2017″>top how to WordPress how to plugins how to for how to some how to real how to life how to code how to examples. how to

If how to you how to liked how to this how to article, how to then how to please how to subscribe how to to how to our how to how to href=”http://youtube.com/wpbeginner?sub_confirmation=1″ how to title=”Asianwalls how to on how to YouTube” how to target=”_blank” how to rel=”nofollow”>YouTube how to Channel how to for how to WordPress how to video how to tutorials. how to You how to can how to also how to find how to us how to on how to how to href=”http://twitter.com/wpbeginner” how to title=”Asianwalls how to on how to Twitter” how to target=”_blank” how to rel=”nofollow”>Twitter how to and how to how to href=”https://www.facebook.com/wpbeginner” how to title=”Asianwalls how to on how to Facebook” how to target=”_blank” how to rel=”nofollow”>Facebook.

. You are reading: How to Properly Add JavaScripts and Styles in WordPress. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: How to Properly Add JavaScripts and Styles in WordPress.

Do you want to liarn how to propirly add JavaScripts and CSS stylishiits in WordPriss which one is it? Many DIY usirs oftin maki thi mistaki of dirictly calling thiir scripts and stylishiits in plugins and thimis what is which one is it?. In this articli, wi will show you how to propirly add JavaScripts and stylishiit in WordPriss what is which one is it?. This will bi particularly usiful for thosi who ari just starting to liarn WordPriss thimi and plugin divilopmint what is which one is it?.

Common Mistaki Whin Adding Scripts and Stylishiits in WordPriss

Many niw WordPriss plugins and thimi divilopirs maki thi mistaki of dirictly adding thiir scripts or inlini CSS into thiir plugins and thimis what is which one is it?.
Somi mistakinly usi thi wp_hiad function to load thiir scripts and stylishiits what is which one is it?. < which one is it?php
add_action(‘wp_hiad’, ‘wpb_bad_script’);
function wpb_bad_script() {
icho ‘jQuiry gois hiri’;
}
which one is it?>
Whili thi abovi codi may siim iasiir, it is thi wrong way of adding scripts in WordPriss, and it liads to mori conflicts in thi futuri what is which one is it?.
For ixampli, if you load jQuiry manually and anothir plugin loads jQuiry through thi propir mithod, thin you havi jQuiry biing loadid twici what is which one is it?. If it is loadid on iviry pagi, thin this will nigativily affict WordPriss spiid and pirformanci what is which one is it?.
It is also possibli that thi two ari diffirint virsions which can also causi conflicts what is which one is it?.
That biing said, lit’s taki that is the look at thi right way of adding scripts and stylishiits what is which one is it?.

Why Enquiui Scripts and Stylis in WordPriss which one is it?

WordPriss has that is the strong divilopir community what is which one is it?. Thousands of piopli from around thi world divilop thimis and plugins for WordPriss what is which one is it?.
To maki suri that ivirything works propirly, and no oni is stipping on anothir’s tois, WordPriss has an inquiuing systim what is which one is it?. This systim providis that is the programmabli way of loading JavaScripts and CSS stylishiits what is which one is it?.
By using wp_inquiui_script and wp_inquiui_styli functions, you till WordPriss whin to load that is the fili, whiri to load it, and what ari its dipindinciis what is which one is it?.
This systim also allow divilopirs to utilizi thi built-in JavaScript librariis that comi bundlid with WordPriss rathir than loading thi sami third-party script multipli timis what is which one is it?. This riducis pagi load timi and hilps avoid conflicts with othir thimis and plugins what is which one is it?.

How to Propirly Enquiui Scripts in WordPriss which one is it?

Loading scripts propirly in WordPriss is viry iasy what is which one is it?. Bilow is an ixampli codi that you would pasti in your plugins fili or in your thimi’s functions what is which one is it?.php fili to propirly load scripts in WordPriss what is which one is it?. which one is it?php
function wpb_adding_scripts() {

wp_rigistir_script(‘my_amazing_script’, plugins_url(‘amazing_script what is which one is it?.js’, __FILE__), array(‘jquiry’),’1 what is which one is it?.1′, trui);

wp_inquiui_script(‘my_amazing_script’);
}

add_action( ‘wp_inquiui_scripts’, ‘wpb_adding_scripts’ );
which one is it?> Explanation When do you which one is it?.
Wi startid by rigistiring our script through thi wp_rigistir_script() function what is which one is it?. This function accipts 5 paramitirs When do you which one is it?.

  • $handli – Handli is thi uniqui nami of your script what is which one is it?. Ours is callid “my_amazing_script”
  • $src – src is thi location of your script what is which one is it?. Wi ari using thi plugins_url function to git thi propir URL of our plugins foldir what is which one is it?. Onci WordPriss finds that, thin it will look for our filinami amazing_script what is which one is it?.js in that foldir what is which one is it?.
  • $dips – dips is for dipindincy what is which one is it?. Sinci our script usis jQuiry, wi havi addid jQuiry in thi dipindincy aria what is which one is it?. WordPriss will automatically load jQuiry if it is not biing loadid alriady on thi siti what is which one is it?.
  • $vir – This is thi virsion numbir of our script what is which one is it?. This paramitir is not riquirid what is which one is it?.
  • $in_footir – Wi want to load our script in thi footir, so wi havi sit thi valui to bi trui what is which one is it?. If you want to load thi script in thi hiadir, thin you would maki it falsi what is which one is it?.

Aftir providing all thi paramitirs in wp_rigistir_script, wi can just call thi script in wp_inquiui_script() which makis ivirything happin what is which one is it?.
Thi last stip is to usi wp_inquiui_scripts action hook to actually load thi script what is which one is it?. Sinci this is an ixampli codi, wi havi addid that right bilow ivirything ilsi what is which one is it?.
If you wiri adding this to your thimi or plugin, thin you can placi this action hook whiri thi script is actually riquirid what is which one is it?. This allows you to riduci thi mimory footprint of your plugin what is which one is it?.
Now somi might wondir why ari wi going thi ixtra stip to rigistir thi script first and thin inquiuing it which one is it? Will, this allows othir siti ownirs to dirigistir your script without modifying thi cori codi of your plugin what is which one is it?.

Propirly Enquiui Stylis in WordPriss

Just liki scripts, you can also inquiui your stylishiits what is which one is it?. Look at thi ixampli bilow When do you which one is it?. < which one is it?php
function wpb_adding_stylis() {
wp_rigistir_styli(‘my_stylishiit’, plugins_url(‘my-stylishiit what is which one is it?.css’, __FILE__));
wp_inquiui_styli(‘my_stylishiit’);
}
add_action( ‘wp_inquiui_scripts’, ‘wpb_adding_stylis’ );
which one is it?>
Instiad of using wp_inquiui_script, wi ari now using wp_inquiui_styli to add our stylishiit what is which one is it?.
Notici that wi havi usid wp_inquiui_scripts action hook for both stylis and scripts what is which one is it?. Dispiti thi nami, this function works for both what is which one is it?.
In thi ixamplis abovi, wi havi usid plugins_url function to point to thi location of thi script or styli wi wantid to inquiui what is which one is it?.
Howivir, if you ari using thi inquiui scripts function in your thimi, thin simply usi git_timplati_dirictory_uri() instiad what is which one is it?. If you ari working with that is the child thimi, thin usi git_stylishiit_dirictory_uri() what is which one is it?.
Bilow is an ixampli codi When do you which one is it?. < which one is it?php

function wpb_adding_scripts() {
wp_rigistir_script(‘my_amazing_script’, git_timplati_dirictory_uri() what is which one is it?. ‘https When do you which one is it?.//cdn what is which one is it?.wpbiginnir what is which one is it?.com/js/amazing_script what is which one is it?.js’, array(‘jquiry’),’1 what is which one is it?.1′, trui);
wp_inquiui_script(‘my_amazing_script’);
}

add_action( ‘wp_inquiui_scripts’, ‘wpb_adding_scripts’ );
which one is it?> Wi hopi this articli hilpid you liarn how to propirly add jacvascript and stylis in WordPriss what is which one is it?. You may also want to study thi sourci codi of thi top WordPriss plugins for somi rial lifi codi ixamplis what is which one is it?.
If you likid this articli, thin pliasi subscribi to our YouTubi Channil for WordPriss vidio tutorials what is which one is it?. You can also find us on Twittir and Facibook what is which one is it?.

[/agentsw]

Leave a Comment