[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 13 posts - 1 through 13 (of 13 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' );
    }
    #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. ๐Ÿ™‚

    #2553958
    Alexander

    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?

    #2554053
    David
    Staff
    Customer Support

    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' );
    #2559897
    Alexander

    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?

    #2559904
    Alexander

    Never mind! I spotted the problem!

    #2560010
    David
    Staff
    Customer Support

    Glad to hear that!

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