Skip to content

🧙‍♂️ Enqueuing Styles and Scripts Like a Wizard

Here we take your WordPress game from "meh" to "magic" with the art of enqueuing! 🪄

Yes, “enqueuing” might sound like something you’d do while waiting in line for your morning coffee ☕, but in WordPress, it’s all about loading styles and scripts in the right place, at the right time, with zero drama.

🎩 What is Enqueuing?

You might be thinking: "Why not just throw a <link> or a <script> tag directly into my header.php?"

Well, you could… but that’s like using a sledgehammer to open a can of soda. It’s messy and leads to unexpected explosions 💥.

Enqueuing is the WordPress-approved way of loading CSS and JavaScript. It prevents conflicts, ensures files load properly, and makes sure your website doesn’t turn into a tangled mess of code spaghetti 🍝.

Step 1: Enqueueing Your Child Theme's Styles

In the previous chapter, we set up our functions.php file to load the parent theme’s styles. But what if you want to add your own styles? Maybe you want to change the color of buttons to a glorious shade of neon pink? 🎀

Here’s how you do it!

Open your child theme’s functions.php, and add the following lines:

php
<?php
function my_cool_child_theme_enqueue_styles() {
    // Load the parent theme's style first
    wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');

    // Load the child theme's style afterwards
    wp_enqueue_style('child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style'));
}
add_action('wp_enqueue_scripts', 'my_cool_child_theme_enqueue_styles');

What just happened?

  • parent-style: First, we load the parent theme’s styles (because we don’t want to reinvent the wheel here).
  • child-style: Next, we load the child theme’s styles and tell WordPress to load them after the parent styles (so your cool customizations take priority).

Basically, it’s like having the parent do the heavy lifting, and then you swoop in to add a little flair at the end 🦸‍♀️.

Step 2: Enqueueing JavaScript in Your Child Theme

Okay, we’ve got the styles covered. Now, what about JavaScript? Maybe you want to add a fancy slider or some cool animations. Let’s make it happen!

Here’s how to enqueue a custom JavaScript file in your child theme.

php
<?php
function my_cool_child_theme_enqueue_scripts() {
    // Load the parent theme's JavaScript files
    wp_enqueue_script('parent-script', get_template_directory_uri() . '/js/script.js', array('jquery'), null, true);

    // Load the child theme's JavaScript files
    wp_enqueue_script('child-script', get_stylesheet_directory_uri() . '/js/custom-script.js', array('parent-script'), null, true);
}
add_action('wp_enqueue_scripts', 'my_cool_child_theme_enqueue_scripts');

Breaking it down:

  • parent-script: Loads the parent theme’s JavaScript.
  • child-script: Loads your custom child theme’s JavaScript, after the parent’s script (so you can modify or extend it as needed).
  • null: This is the version number. You can leave it null unless you want to get fancy.
  • true: This ensures the script is loaded in the footer, so it doesn’t block the page from loading. #SpeedGoals 🚀

🧪 Step 3: Testing Your Enqueue Wizardry

Now that we’ve done all this amazing enqueuing, let’s make sure it actually works!

Test your styles:

  1. Add some CSS to your child theme’s style.css like:

    css
    body {
      background-color: hotpink;
    }
  2. If your site turns into a fabulous pink canvas, congratulations! Your enqueuing skills are on point! 🦄

Test your scripts:

  1. Create a file called custom-script.js in your child theme’s /js/ folder.
  2. Add something simple like:
    javascript
    console.log("Hello from the child theme!");
  3. Check your browser console. If you see that message, you’ve officially nailed enqueuing JavaScript like a wizard 🧙‍♂️.

🎓 In Summary:

  1. Enqueuing is the WordPress way to load CSS and JavaScript files.
  2. Always load the parent theme’s styles and scripts first.
  3. Then, load your child theme’s custom styles and scripts afterwards for maximum control and customization.
  4. Test your styles and scripts to make sure they’re loading properly.

Built by noobs, for noobs, with love 💻❤️