0

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

1 Answer 1

1

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' );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.