Back to Blog
Wordpress enqueue script example7/4/2023 ![]() ![]() That said, it may be a good idea to state all dependencies, just to make sure nothing can break if you forget to include a dependency. Note that even though our second script relies on jQuery, we do not need to specify this, as our first script will already load jQuery. Let’s assume that the first script is a gallery, the second is an extension to that which makes images open up in a lightbox. Wp_enqueue_script( 'custom-gallery-lightbox', plugins_url( '/js/gallery-lightbox.js', _FILE_ ), array( 'custom-gallery' ) ) Here’s an example: add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ) ![]() If you have dependencies of your own, you will need to register them, otherwise your scripts won’t load. You can find a list of scripts and styles available in WordPress in the Codex. We do not need to register or enqueue jQuery ourselves because it is already a part of WordPress. Wp_enqueue_script( 'custom-gallery', plugins_url( '/js/gallery.js', _FILE_ ), array( 'jquery' ) ) Our example above would most probably rely on jQuery so let’s specify that now: add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ) The third parameter is an array of registered scripts/styles that need to be loaded before the current asset is enqueued. You can also use the enqueuing functions right away if you don’t need to separate them. WordPress’ enqueueing mechanism has built-in support for dependency management, using the third argument of both wp_register_style() and wp_register_script() functions. add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ) Īdd_shortcode( 'custom_gallery', 'custom_gallery' ) In a real-world example we could use the wp_enqueue_scripts hook to register the assets and the shortcode’s function to enqueue them. If I were to separate the two functions I would do so by using them in different hooks. Wp_enqueue_script( 'custom-gallery', plugins_url( '/js/gallery.js', _FILE_ ) ) Wp_enqueue_style( 'custom-gallery', plugins_url( '/css/gallery.css', _FILE_ ) ) In fact, you can use the enqueue functions to register and enqueue right away, by using the same arguments as you do in the register functions: add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ) In the example above I registered and enqueued the assets within the same function, which is a bit redundant. Wp_register_script( 'custom-gallery', plugins_url( '/js/gallery.js', _FILE_ ) ) Wp_register_style( 'custom-gallery', plugins_url( '/css/gallery.css', _FILE_ ) ) ![]() add_action( 'wp_enqueue_scripts', 'my_plugin_assets' ) Within the hooked function you can use the wp_register_script(), wp_enqueue_script(), wp_register_style() and wp_enqueue_style() functions. To enqueue scripts and styles in the front-end you’ll need to use the wp_enqueue_scripts hook. Method 1: Include directly in shortcode - This is what I'm currently doing in the plugin, but doesn't seem good since it repeats code.Enqueueing Basics With wp_enqueue_scripts Here are the methods I currently am aware of: I've seen several other approaches on this site and the wp codex even has two examples of their own so it's hard to know what approach is most consistent and fastest. However, I realize now this isn't a good approach since it will repeat the css every time the shortcode is used on a page. I have different style options for displaying the avatar (square, round, etc.) and decided to put the css directly in the shortcode itself. I recently made a plugin simple plugin to add the user avatar/gravatar with a shortcode. ![]() What is the idea way to register/enqueue scripts and/or styles for use in plugins? ![]()
0 Comments
Read More
Leave a Reply. |