maxelcat
maxelcat

Reputation: 1333

adding my own jquery to a wordpress child theme - wp_enqueue_script not working

I am trying to add my own jquery script to a wordpress child theme.

I have created a jquery file: themes/childname/mbn-jq/mbn-jq001.js

I have added this to the functions.php

function mbn001_scripts() {
    wp_enqueue_script( 'mbn001', get_template_directory_uri() . '/mbn-jq/mbn-jq001.js', array(), '1.0.0', true );
}

and I have a function to add_actions to the init action:

add_action( 'init' , 'mh_add_and_remove' , 15 );
function mh_add_and_remove() {
    :
    add_action( 'wp_enqueue_scripts', 'mbn001_scripts' );
}

However, its not working - there is no link to my script in the header.

What am I doing wrong please?

Thanks

Upvotes: 0

Views: 573

Answers (1)

Pieter Goosen
Pieter Goosen

Reputation: 9941

You have two problems here.

PROBLEM 1

get_template_directory_uri() is used for parent themes. The correct path to use in a child theme is get_stylesheet_directory_uri()

PROBLEM 2

You are using the wrong hook. When adding scripts/styles, you should be using the wp_enqueue_scripts hook to hook your function to, not the init hook

Your code should look something like this

function mbn001_scripts() {
    wp_enqueue_script( 'mbn001', get_stylesheet_directory_uri() . '/mbn-jq/mbn-jq001.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'mbn001_scripts' );

Upvotes: 1

Related Questions