0

So i got a parenttheme that enques scripts like this:

function my_scripts() {
    wp_deregister_script('jquery');
    wp_enqueue_script( 'modernizr', get_stylesheet_directory_uri().'/assets/js/dist/modernizr.min.js', array(), '1.0', false );
    wp_enqueue_script( 'jquery', get_stylesheet_directory_uri().'/assets/js/dist/jquery.min.js', array(), '1.0', false );
    wp_register_script( 'scripts', get_stylesheet_directory_uri().'/assets/js/dist/scripts.min.js', array(), '1.0', true );
    wp_enqueue_style( 'fonts', 'https://fonts.googleapis.com/css?family=Lora:400,400italic', array(), '1', 'screen,projection' );
    wp_enqueue_style( 'style', get_stylesheet_directory_uri().'/style.css', array(), '1', 'screen,projection' );

}
add_action( 'wp_enqueue_scripts', 'my_scripts' );

I shuld add that I am using gulp to minify the above scripts and put them in the dist-folder.

Everything works fine in the parent-theme but I have now created a child-theme and the the scripts are not found.

The console logs:

http://localhost:3000/PARENT/wp-content/themes/CHILD/assets/js/dist/jquery.min.js?ver=1.0 - 404

All I have in my child-theme is 2 files:

style.css: /*

 Theme Name:     child
 Description:    parent child theme
 Template:       parent
 Version:        1.0.0
*/

and functions.php:

<?php

add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );

function enqueue_parent_styles() {
 wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}
 ?>

I do not know what causes the error but some thoughts i have had are:

  1. Since im using gulp to minify the scripts, maybe they are not yet created when the child-theme searches for them.

  2. As mentioned above the code looks for the files in: the child-theme:

ttp://localhost:3000/PARENT/wp-content/themes/CHILD/assets/js/dist/jquery.min.js?ver=1.0 -

Maybe it should look for the files in the parent?

Help appreciated. Thank you.

1 Answer 1

1

Thats because parent theme using get_stylesheet_directory_uri() to call script. By this function it is searching for file in your child theme folder (bcause child theme's style.css is using by active theme) where no such files present. This function search those theme folder whose style.css file is using in active theme. Right now your child theme is active (in appearance > theme).

If you want to call that script then use script in your child theme's functions.php like this

function my_scripts() {
    wp_deregister_script('jquery');
    wp_enqueue_script( 'modernizr', get_template_directory_uri().'/assets/js/dist/modernizr.min.js', array(), '1.0', false );
    wp_enqueue_script( 'jquery', get_template_directory_uri().'/assets/js/dist/jquery.min.js', array(), '1.0', false );
    wp_register_script( 'scripts', get_template_directory_uri().'/assets/js/dist/scripts.min.js', array(), '1.0', true );
    wp_enqueue_style( 'fonts', 'https://fonts.googleapis.com/css?family=Lora:400,400italic', array(), '1', 'screen,projection' );
    wp_enqueue_style( 'style', get_template_directory_uri().'/style.css', array(), '1', 'screen,projection' );

}
add_action( 'wp_enqueue_scripts', 'my_scripts' );

In above code I just replace get_stylesheet_directory_uri() with get_template_directory_uri().

get_template_directory_uri() function will search for file in parent theme.

0

Your Answer

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

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