I can't enqueue my scripts - They literally aren't being added to my site

Ok, I've literally copy-pasted different enqueueing code from about 5 different sources (Including wordpress), and not a single one works.

Here's the code I'm using in functions.php:

?php

function my_theme_scripts() {
    wp_register_script(
        'main',
        get_stylesheet_directory_uri() . '/js/main.js',
        array( 'jquery' ), '1.0.0', true
    );
    wp_enqueue_script('main');
}

add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );

?    

and it literally does nothing. No scripts added into my HTML. nothing.

This is my first time using wordpress, and nobody has explained the functions file to me completely right, but from my understanding, i don't do anything like do I?

I've also put ?php wp_footer(); ? right before /body if that means anything.

EDIT: I've solved the problem

Stupidly, I'd forgotten to add ?php wp_head(); ? right before /head, this screwed it all up. I also changed the 'main' identifier to something more unique, which didn't change anything but might be a better practice anyway.

Topic scripts functions jquery Wordpress javascript

Category Web


You should register js file. Please read WordPress codex

https://codex.wordpress.org/Function_Reference/wp_enqueue_script


The codex suggests using the function get_stylesheet_directory() in this instance as the get_template_directory_uri() can get overriden by child themes.

In my experience WordPress can sometimes provide 2 or 3 similar functions.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.