Matt
  • 1

How to Add Custom JavaScript to WordPress?

  • 1

How to enqueue custom js in wordpress?

1 Answer

  1. This answer was edited.

    To enqueue custom JavaScript in WordPress, you can use wp_enqueue_script function. Add your JavaScript file in child theme folder. Lets “custom-script.js” is your file, and it is located in “child-theme/js’ folder, then add the following code in functions.php to enqueue your script.

    function function custom_scripts()
    {
        wp_enqueue_script('custom-script', get_stylesheet_directory_uri() . '/js/custom-script.js', array('jquery'), '1.0', true);
    }
    add_action('wp_enqueue_scripts', 'custom_scripts');

    1. custom-script: The handle for the script, which is used as a unique identifier.

    2. get_stylesheet_directory_uri() . '/js/custom-script.js': The URL of the script file.

    3. array( 'jquery' ): An array of dependencies. If the script not depends on jQuery, then you leave it as empty array( '' ).

    4. 1.0: The version number of the script.

    5. true: Whether to load the script in the footer (true) or the header (false).

Leave an answer

You must login to add an answer.