Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author lilaeamedia

    (@lilaeamedia)

    The best way is to add an action to your child theme functions file. It does not require the plugin, but CTC makes it easy. You should deactivate the child theme while you are editing to prevent syntax errors from breaking your site.

    Go to the files tab and click the link labeled “functions.php.” This will take you to the theme editor.

    After you make the change, test it using the theme preview. View source in your browser to verify the script is being linked correctly. If you errors exist, you are safe to re-activate your child theme.

    Use the following code as a guide to link your script file.

    function my_custom_scripts(){
        wp_enqueue_script( 'my-script-1', get_stylesheet_directory_uri() . '/path/to/script', array( 'jquery' ) );
    }
    add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );

    The actual name of the function is up to you as long as it is unique.

    The first argument of the wp_enqueue_script function is a “handle” that WordPress uses to identify the script in the script queue. It can be any unique string.

    The second argument is the path to the script file. If your javascript file is not located in your child theme, you can use an absolute path instead of the function in the example, such as 'http://www.somedomain.com/path/to/script.js'

    The third argument contains a array of prerequisite script handles (scripts that must load before the script being queued ). In this example we are requiring the jquery library.

    For more information, see:

    http://codex.wordpress.org/Plugin_API
    https://codex.wordpress.org/Function_Reference/wp_enqueue_script
    http://codex.wordpress.org/Child_Themes
    http://codex.wordpress.org/Template_Hierarchy
    http://www.childthemeconfigurator.com/how-to-use/
    http://www.childthemeconfigurator.com/tutorial-videos/
    http://www.childthemeconfigurator.com/how-child-themes-work/

    Hopefully this will get you started.

    Thread Starter bguy0501

    (@bguy0501)

    Thanks so much for the info! I will give that a try.

    Thread Starter bguy0501

    (@bguy0501)

    I replaced the ‘/path/to/script’ with ‘/script.js’ because my js file is in the same folder as my css file. When I view the source I can find both files being linked, but my script still does not work. I couldn’t find anything in the links you provided that jumped out as a possible cause. Is there anything you can think of that I may have missed?

    Thanks for the help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Linking child theme JavaScript file’ is closed to new replies.