1

How to add a custom.js file for a child-theme in a swift way?

By "swift" I mean to a way that won't involve changing anything in the parent theme and will be minimal by means of coping and modifying any settings file from the parent theme to the child theme.

Adding the following code to the add_action function right after wp_enqueue_style function call in functions.php, causes the site to crash (I'm not a PHP programmer and as for now, don't know how to debug this):

wp_enqueue_script( string $handle, string $src = '/custom.js', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false );

This also doesn't bring any effect (even though the site doesn't crash):

wp_enqueue_script( 'custom', get_stylesheet_directory_uri() . '/custom.js');
3

That worked for me:

wp_enqueue_script( 
    'YOUR_SCRIPT_NAME',
    get_stylesheet_directory_uri() . '/YOUR_SCRIPT_FILE.js',
    array(),
    '4.7.3',
    true 
);

Notes:

  1. I usually name the script just as its file name.
  2. You don't have to use the version number 4.7.3, but you do need to write some plausible version number (like of your Wordpress core version) or the keyword null instead.
| improve this answer | |
  • 1
    You can use null instead. I don't have a version number in any of my styles/scripts. – Jack Johansson Apr 9 '17 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy