Warning: include(nectar/meta/meta-config.php) [function.include]: failed to open stream: No such file or directory in /home/content/18/9019618/html/v2014/wp-content/themes/salient/functions.php on line 4650

Warning: include() [function.include]: Failed opening 'nectar/meta/meta-config.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/18/9019618/html/v2014/wp-content/themes/salient/functions.php on line 4650
WordPress Enqueueing Made Easy! A Quick Start Guide To Adding Scripts

WordPress Enqueueing Made Easy! A Quick Start Guide To Adding Scripts And Styles To WordPress

By July 23, 2015 Blog No Comments
WordPress Enqueueing Made Easy! A Quick Start Guide To Adding Scripts

One of the secrets behind the success of WordPress are definitely the countless customization options. It’s a content management system that allows its users to quickly and easily modify the look and feel of their websites, and effortlessly expand the functionalities of WordPress itself. However, in order to unleash the full potential of WordPress and all the customization options it offers, you need to know how to use them effectively. Read on to find out more about WordPress enqueueing and how to add scripts and styles.

What Is WordPress Enqueueing?

There are countless developers writing code for WordPress – whether they’re developing themes or plugins. Think about it, the more plugins you’re running, the better the chance of plugin conflict is, as they’re all using jQuery and other shared scripts. This is where WordPress Enqueueing comes into play and keeps everything running smoothly. By using wp_enqueue_script function, you’re telling WordPress when to load a script, where to load it, and what are its dependencies.

WordPress Enqueueing Made Easy! A Quick Start Guide To Adding ScriptsBuy Theme Open Demo

Why Not Just Load JavaScript In The Header?

If you decide to include JavaScript in the header or the footer, you risk having conflicts with your JavaScript across your installation, such as the situations when multiple plugins are trying to load the same scripts. Another drawback of placing JavaScript in the header is the fact that it will load on every page, whether you need it or not. As a result, the pages will load slower, which is a huge turn-off for web users. WordPress enqueueing gives you much more control over how and when you load your JavaScript.

How to Properly Enqueue

Now that you know what WordPress enqueueing is, and why you should use it, let’s get down to the good stuff and see how you can actually start using it. The process itself is rather simple. Let’s take a look at an example:

< ?php function ava_themes_adding_scripts() { wp_register_script('my_new_script', plugins_url('new_script.js', __FILE__), array('jquery'),'1.1', true); wp_enqueue_script('my_new_script'); } add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' ); ?>

Notice that we used only wp_enqueue_script, and that’s because it works both for scripts and styles. Alternatively, you can also do it like this:

wp_register_style( ‘my-css-style’, get_template_directory_uri() . ‘/css/style.css’, array(), ‘1.0’, ‘all’ );

WordPress Enqueueing Made Easy! A Quick Start Guide To Adding ScriptsBuy Theme Open Demo

And there you have it, everything you need to know about adding scripts and styles to WordPress. Now all that’s left for you to do is go out there and try it yourself. After all, they do say that practice makes perfect.

Leave a Reply