How to add Custom Stylesheets and Scripts to your WordPress Functions.php File

Published 2016-04-19T13:31:58 by Bryan Miller

Whether you’re new to WordPress, or you’ve been creating custom themes for years, you’re probably familiar with a different number of ways to include stylesheets and scripts into your theme. At BRYT DESIGNS, we focus on utilizing the best practices in web design to ensure positive search results, optimized code, and happy developers. As recommended by the WordPress Codex, the preferred method of enqueuing stylesheets and scripts into your custom WordPress theme is via the functions.php file. When you enqueue a stylesheet or script, WordPress will create a handle and a path to find the requested file and any dependencies it may have like jQuery, etc. — and then will use a hook that will insert your stylesheets and scripts.

Why not just include stylesheets & scripts in the header.php file?

In the functions.php file, WordPress utilizes the enqueue system to add external stylesheets and scripts which helps prevent conflicts with other installed plugins. If you are loading your stylesheets and scripts via the header.php file or wp_head function, you are force-loading these files which may cause conflicts with other plugins and break your code.

The benefits of loading your stylesheets & scripts via WordPress functions.php file

WordPress has an extremely strong and expansive web developer community. To ensure compatibility with any other scripts and plugins you may be using, WordPress utilizes the enqueue script function. With this method, you are telling WordPress when you want to load the script, where you want to load it, and what it’s dependencies are.

TLDR: The copy-pasta code

Enqueuing stylesheets and scripts is easy to do, and will help avoid problems for your website down the road. To enqueue your stylesheets and scripts via the functions.php file, place the following code at the bottom of your functions.php file (with your specific needs/modifications of course):

function theme_assets() {

 // Load styles, replace stylesheet-name and stylesheet-path with your files
 wp_enqueue_style( 'stylesheet-name', get_template_directory_uri() . '/stylesheet-path.css' );

 // Load scripts in footer, replace script-name and script-path with your files
 wp_enqueue_script( 'script-name', get_template_directory_uri() . '/script-path.js', '', '', true );

}

add_action( 'wp_enqueue_scripts', 'theme_assets' );

Read more about enqueuing scripts and stylesheets (and add dextra parameters beyond what is explained here) to your WordPress theme at the WordPress.org.

Bryan Miller

Bryan Miller

Bryt Designs

Bryan Miller is an entrepreneur and web tech enthusiast specializing in web design, development and digital marketing. Bryan is a recent graduate of the MBA program at the University of California, Irvine and continues to pursue tools and technologies to find success for clients across a varieties of industries.

Subscribe to our newsletter

STAY UP TO DATE WEB DESIGN, DEV, & SEARCH MARKETING INSIGHTS & TIPS

loading

Ready to make something great?

Let's chat about how we can help achieve your web goals

Let's Chat

Bryt Designs

Web Design, Development, & Search Marketing Insights

Categories

Topics