1
votes

I'm having trouble adding a custom JavaScript file to my child WordPress theme. I'm use the Spacious WordPress theme, and created a Child theme based on it. I have successfully added a custom stylesheet, but so far been unsuccessful in adding a custom JavaScript file. I have followed the instructions provided in the WordPress Developers Handbook on Including CSS & JavaScript.

In my child theme functions.php

...
function spacious_child_enqueue_scripts(){
    wp_register_script('custom_js',get_template_directory_uri().'/js/custom_js.js', array('jquery'), '', true);
    wp_enqueue_script('custom_js');
}
add_action('wp_enqueue_scripts', 'spacious_child_enqueue_styles','spacious_child_enqueue_scripts','get_the_excerpt', 'new_excerpt_more');
...

What am I doing wrong? I can provide additional files and code if needed. Thanks in advance.

2

2 Answers

3
votes

@Her Jossua my mistake sorry. This one should work.

function spacious_child_enqueue_scripts() {
    wp_register_script('custom_js', get_stylesheet_directory_uri().'/js/custom_js.js', array('jquery'), '1.0', true);
    wp_enqueue_script('custom_js');
}

add_action( 'wp_enqueue_scripts', 'spacious_child_enqueue_scripts' );

I was using get_template_directory_uri(); which points to the parent theme directory.

2
votes

@Herr Josua could you try this way?

function spacious_child_enqueue_scripts() {
    wp_register_script('custom_js', get_template_directory_uri().'/js/custom_js.js', array('jquery'), '1.0', true);
    wp_enqueue_script('custom_js');
}

add_action( 'wp_enqueue_scripts', 'spacious_child_enqueue_scripts' );