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.
WordPress snippets using the wp_enqueue_scripts hook
-
submit form
-
create a plugin that allow file format .exe upload
-
wp decision matrix v002
-
sg
-
How to Load jQuery from the Google CDN Using wp_register_script in WordPress
-
How to Conditionally Load a Stylesheet Only When a Gallery is Used in WordPress
-
How to Enable Contact Form 7 on Specified Pages Only & Otherwise wp_deregister_script
-
How to Display Subpages in the Sidebar on WordPress
-
How to Force Reload Scripts and Stylesheets in Your WordPress Plugin or Theme
-
How to Add a Slider in WordPress: Step-by-Step Guide