wp_enqueue_scripts

Home » Hooks » wp_enqueue_scripts

If you’re a developer working with WordPress, you’ve probably heard about the importance of keeping your code organized and efficient. One way to do this is by using hooks – special functions that allow you to modify the behavior of WordPress core code or other plugins and themes.

One of the most commonly used hooks is wp_enqueue_scripts. This hook is used to add scripts and styles to the front-end of your WordPress website, which can improve page load times and overall performance. By using wp_enqueue_scripts, you can ensure that scripts and styles are only loaded when they’re needed, reducing the amount of HTTP requests required to load your website.

Here’s an example of how to use wp_enqueue_scripts:

function my_scripts() { wp_enqueue_style( ‘my-style’, get_stylesheet_directory_uri() . ‘/style.css’ ); wp_enqueue_script( ‘my-script’, get_stylesheet_directory_uri() . ‘/script.js’, array(), ‘1.0.0’, true ); } add_action( ‘wp_enqueue_scripts’, ‘my_scripts’ );

In this example, we’re using the wp_enqueue_style and wp_enqueue_script functions to add a stylesheet and JavaScript file to our website. The first parameter is the name of the file, followed by the URL to the file. You can also specify dependencies, version numbers, and whether the file should be loaded in the header or footer of your website.

By using wp_enqueue_scripts, you can keep your code organized and ensure that your website is running efficiently.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.