Enqueuing styles and scripts in WordPress helps you add CSS and JavaScript files safely and properly to your website. It avoids conflicts and ensures files load in the right order.
Enqueuing styles and scripts in Wordpress
Start learning this pattern below
Jump into concepts and practice - no test required
<?php
function my_theme_enqueue() {
wp_enqueue_style('handle-name', get_template_directory_uri() . '/css/style.css', array(), '1.0', 'all');
wp_enqueue_script('handle-name', get_template_directory_uri() . '/js/script.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue');wp_enqueue_style adds a CSS file.
wp_enqueue_script adds a JavaScript file.
<?php
wp_enqueue_style('main-style', get_stylesheet_uri());<?php wp_enqueue_script('custom-js', get_template_directory_uri() . '/js/custom.js', array(), '1.2', true);
<?php wp_enqueue_style('google-fonts', 'https://fonts.googleapis.com/css?family=Roboto', array(), null);
This code adds a CSS file named my-style.css and a JavaScript file named my-script.js to your WordPress theme. The script depends on jQuery and loads in the footer for better performance.
<?php
function enqueue_my_assets() {
wp_enqueue_style('my-style', get_template_directory_uri() . '/css/my-style.css', array(), '1.0', 'all');
wp_enqueue_script('my-script', get_template_directory_uri() . '/js/my-script.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'enqueue_my_assets');Always use wp_enqueue_style and wp_enqueue_script instead of hardcoding links in header or footer.
Use the wp_enqueue_scripts action hook to add your styles and scripts.
Setting the last parameter of wp_enqueue_script to true loads the script in the footer, which helps page speed.
Enqueuing is the safe way to add CSS and JS files in WordPress.
Use wp_enqueue_style for styles and wp_enqueue_script for scripts.
Hook your enqueue function to wp_enqueue_scripts to load files properly.
Practice
wp_enqueue_style and wp_enqueue_script in WordPress?Solution
Step 1: Understand the function purpose
wp_enqueue_styleandwp_enqueue_scriptare designed to add CSS and JS files safely to WordPress pages.Step 2: Compare options
Options B, C, and D describe incorrect uses: printing code directly, removing files, or creating files automatically, which these functions do not do.Final Answer:
To safely add CSS and JavaScript files to a WordPress theme or plugin -> Option CQuick Check:
Enqueuing = Safe adding of styles/scripts [OK]
- Thinking enqueue prints code directly
- Confusing enqueue with file creation
- Using enqueue to remove scripts
style.css located in your theme folder?Solution
Step 1: Identify correct function and parameters
The correct function to enqueue CSS iswp_enqueue_style. The URL should be built withget_template_directory_uri()plus the file path.Step 2: Check each option
wp_enqueue_style('my-style', get_template_directory_uri() . '/style.css'); uses the correct function and URL. enqueue_style('my-style', '/style.css'); uses a wrong function name. wp_enqueue_script('my-style', get_stylesheet_directory() . '/style.css'); useswp_enqueue_scriptwhich is for JS, not CSS. wp_add_style('my-style', get_template_directory_uri() . '/style.css'); uses a non-existent functionwp_add_style.Final Answer:
wp_enqueue_style('my-style', get_template_directory_uri() . '/style.css'); -> Option BQuick Check:
Correct function and URL = wp_enqueue_style('my-style', get_template_directory_uri() . '/style.css'); [OK]
- Using wp_enqueue_script for CSS files
- Wrong function names like enqueue_style
- Using get_stylesheet_directory() without URI
functions.php?
function load_custom_scripts() {
wp_enqueue_script('custom-js', get_template_directory_uri() . '/js/custom.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'load_custom_scripts');Solution
Step 1: Analyze the enqueue parameters
The function enqueues 'custom-js' with dependency on 'jquery', version '1.0', and last parameter true means load in footer.Step 2: Understand the hook and dependencies
The function is hooked to 'wp_enqueue_scripts', so it runs properly. jQuery will load before custom.js because of the dependency.Final Answer:
The custom.js script will be loaded in the footer after jQuery -> Option AQuick Check:
Dependency + footer true = load after jQuery in footer [OK]
- Confusing footer true with header loading
- Forgetting to hook function to wp_enqueue_scripts
- Ignoring script dependencies
function add_my_script() {
wp_enqueue_script('my-script', get_template_directory() . '/js/script.js');
}
add_action('wp_enqueue_scripts', 'add_my_script');Solution
Step 1: Check the URL function used
get_template_directory()returns a server path, not a URL. For enqueuing scripts, a URL is needed, soget_template_directory_uri()should be used.Step 2: Verify other parts
The version parameter is optional, so missing it is not an error. Hooking towp_enqueue_scriptsis correct.add_actionis the right function to hook.Final Answer:
Using get_template_directory() instead of get_template_directory_uri() for URL -> Option DQuick Check:
Use URI function for URLs, not directory path [OK]
- Using server path instead of URL for script source
- Confusing hooks like wp_head vs wp_enqueue_scripts
- Thinking version parameter is mandatory
main.css and theme.css. theme.css depends on main.css. Which code correctly enqueues both with dependency and versioning?Solution
Step 1: Understand dependency order
theme.cssdepends onmain.css, sotheme-stylemust listmain-styleas a dependency.Step 2: Check versioning and parameters
wp_enqueue_style('main-style', get_template_directory_uri() . '/main.css', array(), '1.0'); wp_enqueue_style('theme-style', get_template_directory_uri() . '/theme.css', array('main-style'), '1.1'); correctly enqueuesmain-stylefirst with no dependencies, thentheme-stylewithmain-styleas dependency and proper versions. Other options reverse dependencies or omit them.Final Answer:
wp_enqueue_style('main-style', get_template_directory_uri() . '/main.css', array(), '1.0'); wp_enqueue_style('theme-style', get_template_directory_uri() . '/theme.css', array('main-style'), '1.1'); -> Option AQuick Check:
Dependency array lists main-style for theme-style [OK]
- Reversing dependency order
- Omitting dependency array
- Not specifying versions when needed
