Despite the name, it is used for enqueuing both. If you want to add scripts and styles into your WordPress installation, there are different ways of doing the same. The enqueue function takes three parameters. As a plugin developer, you do not have ready access to the header template, but this rule bears mentioning anyway. Never hardcode such links in the header template. load parent stylesheet first if this is a child theme wpenqueuescripts is the proper hook to use when enqueuing items that are meant to appear on the front end. Enqueue Use the function wpenqueuescript () to get WordPress to insert a meta link to your script in the page’s section. To make a theme compatible with child themes, you can use functions to determine the correct path to the core stylesheets ( without using in the CSS ): Wp_enqueue_script( 'my-js', 'filename.js', false ) Īdd_action( 'wp_enqueue_scripts', 'themeslug_enqueue_style' ) Īdd_action( 'wp_enqueue_scripts', 'themeslug_enqueue_script' ) Don’t forget to change the script and style handle name and path to the file.
#WORDPRESS ENQUEUE SCRIPT CODE#
Add this code to functions.php file to add scripts and styles in WordPress admin for all pages. This hook will be the same for both scripts and style. ‘enqueuescripts’) this must come second, it’s the name you give your custom function. (‘wpenqueuescripts, this must come first, it’s the built in WordPress function. Pretty ugly, but perhaps marginally better than echoing the script, as it'll still use the WordPress. Wp_enqueue_style( 'core', 'style.css', false ) To enqueue any scripts and styles in admin, WordPress provides a hook adminenequeuescripts. addaction ('wpenqueuescripts', 'enqueuescripts') The final part of this code snippet is telling WordPress to insert the scripts we referenced in our function. addaction('adminenqueuescripts', 'loadgooglemaps').
Despite the name, it is used for enqueuing both scripts and styles. The first hook is intended for loading scripts and stylesheets that affect the frontend of the site, while the second is for those affecting the admin dashboard.Wp_enqueue_scripts is the proper hook to use when enqueuing items that are meant to appear on the front end. The second represents the use of the add_action function which hooks on one of the two default WordPress script and stylesheet loading hooks – wp_enqueue_scripts and admin_enqueue_scripts. This means you can register a script on the hook wpenqueuescripts and then only actually load it when it's needed. The first piece is a function that enqueues the file with the use of wp_enqueue_script and wp_enqueue_style. +1 Note since 3.3 you can use wpenqueuescript inside shortcode/widget callbacks (or otherwise in the body of the page). The code snippet contains two distinct pieces of code. You need to insert it into the functions.php file of your parent or child theme or inside a plugin file, depending on the location of your custom script/stylesheet file. Afterward, create a snippet of code that enqueues the file. Then, place the file either inside your theme or plugin directory. First, you need to create a separate file for your code. Now that we’ve come to grips with what enqueueing and hooks are, we can move on to adding scripts and stylesheets to WordPress.