
It also reduces the likelihood that our plugins scripts and styles will conflict with another plugin or theme. The reason why is we don’t want to slow down the site our plugin is installed on with unnecessary scripts or styles. This is done for DB2 MSTR, DBM1, DIST, and IRLM. It’s a good practice to only load the styles and scripts in your plugin when they are needed. The exception system invokes GQSCAN and parses the output to find any ENQs that are waiting on another holder.
#Enqueue script update#
We include the version for the same reason as before so that the user’s cache can be cleared if we update the stylesheet. We have a unique handle, the URL to the stylesheet, dependencies (in this case we have none), and the version of the stylesheet. Settings for this function are similar to wp_register_script. Stylesheets can easily be added using wp_enqueue_style. We use this so that other plugins or the site owner can remove our script if they want to without having to touch our plugin’s code. The wp_enqueue_script() function is what allows WordPress to process your script and output it where it is needed.

In this case, ours is dependent on jQuery.

#Enqueue script code#
The proper use of the hook includes making a function with the enqueueing code first and then adding an action with the hook and function name as two of the arguments used. In this case, we’re using a file called script.js, which is located in the same folder as the file that is adding this function. The WordPress hook for enqueueing scripts and stylesheets meant to be used for your admin dashboard is called adminenqueuescripts. I am working on a plugin that has to inject a script on pages (something like RefTagger), which would be easy using wpenqueuescript, but there are two reasons I cannot do that: the script tag has to have an ID and it should be executed asynchronously. Source: This is the location of the file we want to load.This helps makes sure the script is only loaded once. This is the unique name of the script so that WordPress can identify it. Handle: This is the ‘sd_my_cool_script’ portion.There are five pieces to the wp_register_script function. Wp_enqueue_style( 'sd_my_styles', plugins_url( 'styles.css', _FILE_ ), '', '1.0' ) Īdd_action( 'wp_enqueue_scripts', 'sd_add_scripts' ) Wp_enqueue_script( 'sd_my_cool_script' ) Wp_register_script( 'sd_my_cool_script', plugins_url( 'script.js', _FILE_ ), array( 'jquery' ), '1.0', true ) 'style.Function sd_load_script ( ) Īdd_action ( 'wp_enqueue_scripts', 'sd_add_scripts' ) If you need to enqueue scripts for your custom widgets. Wp_enqueue_style( 'parent-stylesheet', trailingslashit( get_template_directory_uri() ). This action gives you a chance to enqueue any additional frontend scripts and styles for a widget. load parent stylesheet first if this is a child theme 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' ) Wp_enqueue_style( 'core', 'style.css', false ) You can first enqueue your script using wpenqueuescript ( your-script-handle, script-file. Despite the name, it is used for enqueuing both scripts and styles.


Wp_enqueue_scripts is the proper hook to use when enqueuing items that are meant to appear on the front end.
