- This topic has 12 replies, 4 voices, and was last updated 1 year, 1 month ago by David.
-
AuthorPosts
-
January 12, 2021 at 6:09 am #1615613Davide
Hello there! I am trying to enqueue my scripts in child
functions.php
withfunction 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?
January 12, 2021 at 8:51 am #1616018DavidStaffCustomer SupportHi 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
January 12, 2021 at 9:55 am #1616081DavideHello 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, whileget_template_directory_uri()
as in the example points to the parent theme root folder.January 13, 2021 at 8:17 am #1617330DavidStaffCustomer SupportTry 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'); }
January 14, 2021 at 1:53 pm #1619102DavideThanks, 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.
January 14, 2021 at 6:23 pm #1619306ElvinStaffCustomer SupportSince 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' ); }
January 15, 2021 at 7:31 am #1620144DavideLovely, 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'); } }
January 17, 2021 at 5:21 pm #1622848ElvinStaffCustomer SupportNice one. Glad you got it sorted. ๐
March 3, 2023 at 5:11 am #2553958AlexanderHello 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?
March 3, 2023 at 7:07 am #2554053DavidStaffCustomer SupportHi 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' );
March 8, 2023 at 4:45 am #2559897AlexanderHi 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?
March 8, 2023 at 4:55 am #2559904AlexanderNever mind! I spotted the problem!
March 8, 2023 at 6:32 am #2560010DavidStaffCustomer SupportGlad to hear that!
-
AuthorPosts
- You must be logged in to reply to this topic.