1
votes

I have some trouble making wp_enqueue_script and wp_enqueue_style work..

In the begining of functions.php file, I added this function :

<?php

require_once ....

[....]

add_action('wp_enqueue_scripts', 'my_enqueueScripts');
function my_enqueueScripts(){

    wp_register_script('my_js_script', get_template_directory_uri().'/myDirectory/my_js.js', array('jquery'), '1.0', true);
    wp_enqueue_scripts('my_js_script');

    wp_register_style('my_css_script', get_template_directory_uri().'/myDirectory/my_css.css', false, '1.0', 'all');
    wp_enqueue_style('my_css_script');
}

[....]

<?php

This does not work... My scripts are not loaded and they are not in the "sources" files of my website page...

I tried to put my css file in templates/thetemplate/css/ and my js file on in templates/thetemplate/js/ but this does not solved the problem.

In the template header.php file, the wp_head(); call is here. And in the footer.php file, the wp_footer(); is also here.

I don't know how to make this work....Do I miss something ?

Thanks in advance for your help...

1
Are you sure the path is correct?Vidya L
I tried to echo get_template_directory_uri().'/myDirectory/my_js.js'; and the display on the page is correct...Mélanie
Are you using a child theme? Have you tried printing out a message to make sure my_enqueueScripts is actually getting called?FluffyKitten

1 Answers

0
votes

Try this

add_action('wp_enqueue_scripts', 'my_enqueueScripts');
function my_enqueueScripts(){

    wp_enqueue_script('my_js_script', plugins_url('/your-plugin-folder/your-subfolder/my_js.js'),   array('jquery') );

    wp_register_style( 'my_css_script', plugins_url('...' ) );
    wp_enqueue_style( 'my_css_script' );  

}

If it does not work something's wrong with your paths. The code above is correct but it doesn't locate the file using plugins_url() or get_template_directory_uri() properly. You could try debugging if you echo the two functions and analyze the urls/paths.

Edit: If you are working with a theme and not a plugin plugins_url() might not work (I am not 100% sure).