-1

I have tested the html and the css and the js independently of wordpress and all works fine. So trying to integrate it into wordpress. I have added the html code as a code module. I have created a folder in the main theme dir (not the child) called 'js' and have included the following into the functions.php

add_action( 'wp_enqueue_scripts', 'my_enqueue_assets' ); 

function my_enqueue_assets() { 

    wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' ); 
    wp_register_script( 'portfolio', get_template_directory_uri() . '/js/portfolio.js', '', null,''  );
    wp_register_script( 'index', get_template_directory_uri() . '/js/index.js', '', null,''  );
    wp_register_script( 'config', get_template_directory_uri() . '/js/config.js', '', null,''  );

    wp_register_script( 'jquery.colorbox-min', get_template_directory_uri() . '/js/jquery.colorbox-min.js', '', null,''  );
    wp_enqueue_script( 'jquery.colorbox-min' );
    wp_enqueue_script( 'portfolio' );
    wp_enqueue_script( 'index' );
    wp_enqueue_script( 'config' );
}

I am not sure if this code is correct but hope that someone can give me some advice on why it doesn't work.

21
  • If you use child theme, why you create folder in main theme?
    – Jevuska
    Commented Mar 19, 2016 at 13:05
  • i just wanted to have a look , i have the same js folder in both child and parent now to test... any ideas ? Commented Mar 19, 2016 at 13:09
  • why it doesn't work Is always the hard question. Narrow down the problem by specifying, do you see scripts in page source? URLs are correct? Or you do not see at all ?
    – Sumit
    Commented Mar 19, 2016 at 13:16
  • urls correct , is the code above syntax correct ??? Commented Mar 19, 2016 at 13:17
  • yes , using a divi theme on my webspace , have child and parent , the above code is the functions.php file , the javascript files are also on both child and parent locations now so confused , the above code is not right for some reason but not sure why Commented Mar 19, 2016 at 13:21

1 Answer 1

0

You might have to use get_stylesheet_directory_uri() instead of get_template_directory_uri() when you're referencing files in a child theme.

On a fresh install, I created and activated a child theme of twentyfifteen called child and run the following codes:

echo get_stylesheet_directory_uri(); 
# Output: http://localhost/test1/wp-content/themes/child

echo get_template_directory_uri(); 
# Output: http://localhost/test1/wp-content/themes/twentyfifteen

Also, If you have tested the JS independently of WordPress, you should know that WordPress loads jQuery in the no conflict mode, that is, you should use jQuery instead of $.

So:

$(document).ready(function(){
    // JS code
});

Becomes:

jQuery(document).ready(function($){
    // JS code
});

Your Answer

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

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