Create Custom Menu in WordPress Dashboard
Complete tutorial on create custom menu in wordpress dashboard. Discover practical examples, implementation tips, and expert advice for WordPress and WooCo
Read More โHardcoding `<link>` and `<script>` tags into your header or footer is a common mistake that leads to plugin conflicts and broken dependencies. The WordPress "Enqueue" system is designed to solve this by acting as a central registry. By using `wp_enqueue_script` and `wp_enqueue_style`, you can specify exactly when a file should load (like after jQuery) and where (header vs. footer). This standard approach ensures your theme remains compatible with the wider WordPress ecosystem and improves page load performance through proper asset handling.
The correct way to include JavaScript and CSS in WordPress is through the wp_enqueue_scripts hook. This prevents duplication and ensures compatibility with themes and plugins.
Why Use Enqueuing Instead of Hardcoding?
Using proper WordPress functions to load assets ensures:
With wp_enqueue_script() and wp_enqueue_style(), your files are added cleanly to the frontend.
How to Add Scripts and Styles
Insert this in functions.php to load your CSS and JS:
function theme_assets_loader() {
wp_enqueue_script('theme-main-js', get_template_directory_uri() . '/assets/js/main.js', array('jquery'), '1.0', true);
wp_enqueue_style('theme-main-css', get_template_directory_uri() . '/assets/css/main.css', array(), '1.0');
}
add_action('wp_enqueue_scripts', 'theme_assets_loader');
Code Breakdown
Make sure your theme has the proper folder structure. Correct enqueuing helps prevent plugin clashes and improves page speed.
Final Thoughts
Use wp_enqueue_scripts for all frontend asset loading in WordPress. This is the recommended method for a robust, performance-friendly site. See the official docs to explore more. 
Search our archives or reach out to our team for solutions and expert advice.