![]() 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. If you have registered the script or style using wp_enqueue_scripts it will only be available on the front end of your site and not the admin pages or the login pages.Enqueueing Basics With wp_enqueue_scripts You don’t need to enqueue the script directly after registering as a practice, however after registering the script you can enqueue it on pages you require it. In the example above you can see first I have registered the script then used wp_enqueue_script to enqueue it. Wp_enqueue_scripts hook – Front End function themeslug_enqueue_style () WordPress takes care of these problems when you use the hooks intended.Īn example of the three enqueuing hooks are below: This can cause conflicts with other scripts, plugins or themes. When you don’t enqueue scripts using these hooks and instead use the tag, it can cause the script to be loaded every time you load the site, admin area or login page. This allows WordPress to load only the scripts needed for that specific section. For example, if a script was enqueued using the login_enqueue_scripts hook that script would not be loaded in the admin area. There are three different areas that a developer can enqueue scripts for the first is the client side, the second is the admin side and the third is the login pages.Įach of these areas uses a specific hook to enqueue scripts to use in that area. ![]() This can be a really confusing process for new WordPress developers. In this tutorial I want to explain the different methods to enqueue scripts and style sheets in WordPress.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |