1/4/2024 0 Comments Enqueue javascriptThe same rule applies to any other dependency that your asset has. JQuery is already included in WordPress, so we don’t need to load it separately. Wp_enqueue_script( 'my-portfolio', plugins_url( '/js/portfolio.js', _FILE_ ), array( 'jquery' ) ) We can do so by adding “array (‘jquery’)” to the enqueue script function. In the example above, we’d need to load wp_enqueue_script jQuery first. The third parameter is an array of registered scripts and styles that load first before enqueueing our desired asset. WordPress has a built-in way of managing dependencies through the third argument of both the wp_register_style() and wp_register_script() function. Wp_enqueue_script( 'my-portfolio', plugins_url( '/js/portfolio.js', _FILE_ ) ) Wp_enqueue_style( 'my-portfolio', plugins_url( '/css/portfolio.css', _FILE_ ) ) You can also use the enqueue function to register and enqueue the scripts at once. The example above shows how to register and enqueue the assets within the same function in two steps. Wp_register_script( 'my-portfolio', plugins_url( '/js/portfolio.js', _FILE_ ) ) Wp_register_style( 'my-portfolio', plugins_url( '/css/portfolio.css', _FILE_ ) ) Here’s a practical wp_enqueue_script example of how this would look in the style.css file:Īdd_action( 'wp_enqueue_scripts', 'custom_plugin_assets' ) Within the action hook, you can use several functions and embed them in the functions.php file: Let’s go through the basics of the wp_enqueue_script hook that you’ll use to load your assets. We now know what enqueueing is and how it works. This lets WordPress save on resources and speeds up your website. To achieve the desired result, you can register the script first, then enqueue it to include the shortcode on only the required pages. You need a product portfolio on only your services page or shop page. Not all WordPress pages benefit from that. For example, you might be building a shortcode that outputs a simple portfolio of popular products. You might need to include an asset on only specific pages. WordPress then automatically outputs the asset in the appropriate location: the header or the footer. How Enqueueing Works on WordPressįirst, you register your script or style: tell WordPress your asset is present.Īfter telling WordPress your asset is there, the second step is to enqueue it. WordPress will then automatically link those assets in the header and footer. Enqueueing on WordPressĮnqueueing is the process of loading Javascript files or JS files - including scripts and styles - to WordPress in a way that lets you use them whenever you need them without requiring the rewriting of code.īy enqueueing scripts, you tell WordPress which assets you want to add. In this post, we’ll explain what enqueueing is, how it works, and how to properly use this process to add the necessary scripts and styles to your WordPress theme. The proper way is to use a method called enqueueing. The wrong way is to add them to the WordPress header file or wp_head. While it helps you style a theme and add extra features, there is a wrong and a right way of adding styles and scripts to WordPress. One of the best things about WordPress is that you have thousands of themes available to enhance your website’s front end.Īnd, no matter what type of WordPress theme you have, you’ll use styles and scripts to enhance the look and functionality of your website. Every WordPress website needs a theme to function properly.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |