-1

I intended to load a script within a child's theme because I wanted to experiment with a feature. Initially, I loaded the script as follows:

// loading menu toggle function
function menu_toggle_enqueue_script() {
    wp_enqueue_script( 'menu-toggle', get_template_directory_uri().'/assets/js/menu.js');
}
add_action('wp_enqueue_scripts', 'menu_toggle_enqueue_script');

Unfortunately, that returned an error.

  • What error? Please be as specific as possible. – fuxia May 21 at 9:09
-1

The reason being that:

get_template_directory_uri() actually returns the parent's theme url.

To fix it, I simply concatenated the remainder of the path. In this case, the code became:

// loading menu toggle function | located in the child's theme folder
function menu_toggle_enqueue_script() {
    wp_enqueue_script( 'menu-toggle', get_template_directory_uri(). '-child' . '/assets/js/menu.js');
}
add_action('wp_enqueue_scripts', 'menu_toggle_enqueue_script');

I figured out the solution on my own and thought I would share it since I did not find this solution anywhere I researched.

| improve this answer | |
  • (You've also managed to post this answer using a different account with the same name. You can get them merged for the future.) – Rup May 19 at 15:56
  • 1
    @Rup Better yet, use get_theme_file_uri(). – Jacob Peattie May 20 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.