Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript I need help getting my JavaScript file to work on wordpress through child theme?

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #159986
    iizag
    Participant

    Hi , So I looked at a video about linking a JS file to my wordpress theme, and was a little confused since it was in reference to including a sliders JS . BUT This is my interpretation please let me know if it is correct. So I have the file called myscript.js in my child theme now I want to “activate it for use” so in my child themes functions php file I put the following :

    // Function to load   javascript
    function theme_javascript() {
    
        // Register script
        wp_register_script('myscript',get_template_directory_uri().'js/jquery.myscript.js',array('jquery'),'1.0');
    
        // Enqueue theme script
        wp_enqueue_script('myscript',get_template_directory_uri().'js/theme.js',array('jquery','myscript'),'1.0');
    
    } 
    

    There has to something wrong because its not working ? Thank you for any help

    #159990
    Alen
    Participant

    You do not need to register your script. From documentation:

    You could either link a script with a handle previously registered using the wp_register_script() function, or provide this function with all the parameters necessary to link a script.

    function theme_javascript()
    {
        // wp_enqueue_script( 'name', path, array( 'dependencies' ), version, in-footer );
        wp_enqueue_script( 'myscript', get_template_directory_uri() . '/js/myscript.js', array('jquery'), '20140109', true );
    }
    
    add_action( 'wp_enqueue_scripts', 'theme_javascript' );
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.