Basic (I think?) wp_enqueue issue

I am trying to load a script called accordion.js using this:

function accordion() {
    wp_register_script( 
        'accordion',  
        get_template_directory_uri() . '/js/accordion.js',
        array('jquery'),
        null, 
        true 
        );
    wp_enqueue_script('accordion');
}

It’s not loading the script (as in it doesn’t appear in Resources with Chrome devtools), and I can’t figure out what I’m doing wrong. I realize this is probably very basic, but nothing I’m trying seems to fix it.

Related posts

Leave a Reply

1 comment

  1. The snippet you’ve included will not work on its own (if you’ve dropped that into your functions.php file). You’ll also need to ensure that you call your function with the proper hook like so:

    add_action('wp_enqueue_scripts', 'accordion');
    

    This will tell WordPress to run the accordion function when it’s time to enqueue the scripts.

    Additionally, you might want to rename your function to something more generic such as load_resources and you can enqueue all of your scripts that you’ll need inside that function.

    If you’re doing that successfully, but you still have problems, you might check the Net tab of your developer tool and see if the browser is at least attempting to load the file (if even from the wrong location).