[Resolved] Cannot enqueue custom js via php

Home Forums Support [Resolved] Cannot enqueue custom js via php

Home Forums Support Cannot enqueue custom js via php

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1615613
    Davide

    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?

    #1616018
    David
    Staff
    Customer Support

    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

    #1616081
    Davide

    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.

    #1617330
    David
    Staff
    Customer Support

    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');
    }
    #1619102
    Davide

    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.

    #1619306
    Elvin
    Staff
    Customer Support

    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' );
    }

    A wise man once said:
    "Have you cleared your cache?"

    #1620144
    Davide

    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');
    	}
    }
    
    #1622848
    Elvin
    Staff
    Customer Support

    Nice one. Glad you got it sorted. ๐Ÿ™‚

    A wise man once said:
    "Have you cleared your cache?"

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.