Archived topic
Cannot enqueue custom js via php
12 replies · Started by Davide on January 12, 2021
Hello there! I am trying to enqueue my scripts in child functions.php with
function custom_js() {
wp_enqueue_script( 'theme_js', get_stylesheet_directory_uri() . '/js/davide_scripts.js', array( 'jquery' ), '1.0', true );
echo(get_stylesheet_directory_uri() . '/js/davide_scripts.js'); // Debugging, check path to be correct
}
add_action( 'wp_enqueue_scripts', 'custom_js' );
but it does not work. The same .js file hooked to wp_head with
<script type="text/javascript" src='https://softgunners.net/wp-content/themes/generatepress_child/js/davide_scripts.js'> </script>
works fine.
Any suggestion?
Hi there,
your wp_enqueue_script arguments are incorrect, see here for an example of the correct method:
https://developer.wordpress.org/reference/functions/wp_enqueue_script/#comment-274
Hello David,
why are my arguments incorrect? They are the same as in the example you provided, but for the path: get_stylesheet_directory_uri() poins to the child theme root folder, where my file is located, while get_template_directory_uri() as in the example points to the parent theme root folder.
Try this method:
add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
wp_register_script(
'theme_js',
get_stylesheet_directory_uri() . '/js/davide_scripts.js',
array('jquery')
);
wp_enqueue_script('my-accordion-script');
}
Thanks, this works!
Since now both options are available (wp_enqueue_scripts and src hook in wp_head), which one is better? Consider I only need the script for one post category, and the hook allows me to load it only for that category.
Since now both options are available (wp_enqueue_scripts and src hook in wp_head), which one is better? Consider I only need the script for one post category, and the hook allows me to load it only for that category.
You can set a condition for add_action( 'wp_enqueue_scripts', 'add_my_script' ); so it only loads on certain category.
Example: (For single posts under category "test_category")
if(is_singular() && in_category('test_category')){
add_action( 'wp_enqueue_scripts', 'add_my_script' );
}
Lovely, thanks a lot! 😀
Edit: for future reference, the provided code did not actually work: I had to put the if statement inside the function to make it work (https://wordpress.stackexchange.com/questions/310865/is-singular-and-is-home-not-working)
add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
if(is_singular() && in_category('Recensioni')) {
wp_register_script(
'review_grid_js',
get_stylesheet_directory_uri() . '/js/review-grid.js',
array('jquery')
);
wp_enqueue_script('review_grid_js');
}
}
Nice one. Glad you got it sorted. :)
Hello all, I may tie here briefly.
I just can't get my script to embed. The script is in the child-theme-folder under js/voting.js
Here is my code:
// Funktion zum Enqueue der Javascript-Datei
add_action( 'wp_enqueue_scripts', 'add_my_script' );
function add_my_script() {
wp_register_script(
'voting_js',
get_stylesheet_directory_uri() . '/js/voting.js',
array('jquery')
);
wp_enqueue_script('voting_js');
}
What am I doing wrong? Am I missing something?
Hi there,
try just enqueueing your script:
function add_my_script() {
wp_enqueue_script( 'voting_js', get_stylesheet_directory_uri() . '/js/voting.js', array('jquery'), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'add_my_script' );
Hi David,
I have now adapted the code again. Also I have completely copied a code that I have running on another site. The page vehemently refuses to load my Custom.js. I don't know what the problem is. Maybe you can have a look. How can I write private content in the forum?
Never mind! I spotted the problem!
Glad to hear that!