Adding Custom Javascript to Skeleton Child Theme

I’m trying to register & enqueue ImageMapster in my Skeleton Child Theme on the main.php page to make an image map with hovering image effects. I added this code to the “Header Functions” section of my child theme’s functions.php file:

add_action( 'wp_enqueue_scripts', 'load_custom_js' );
function load_custom_js() {
    wp_register_script( 'jquery.imagemapster.js', 
    get_template_directory_uri() . '/javascripts/jquery.imagemapster.js', 
    array('jquery'), '1.2.6', false );

    if ( is_page('Main') ) {
    wp_enqueue_script( 'jquery.imagemapster.js', 
    get_template_directory_uri() . '/javascripts/jquery.imagemapster.js', 
    array('jquery'), '1.2.6', false );
    }
} 

Now my site has ceased to work. The javascript file is located in the parent theme directory, so is it correct to use get_template_directory_uri instead of get_stylesheet_directory_uri? Does each theme have a particular place where one should add custom functions or am I correct in adding it under “Header Functions”? Any help would be appreciated.

2 Answers
2

One problem: if you define the script in a wp_register_script() like so:

wp_register_script( 
    'jquery.imagemapster.js', 
    get_template_directory_uri() . '/javascripts/jquery.imagemapster.js', 
    array('jquery'), 
    '1.2.6', 
    false 
);

…then you only need to reference the defined script’s name in the wp_enqueue_script() call, like so:

wp_enqueue_script( 'jquery.imagemapster.js' );

Re-declaring all of the parameters in wp_enqueue_script() shouldn’t cause the fatal error you describe; however, it could cause unintended consequences (especially if the declarations do not match between wp_register_script() and wp_enqueue_script()).

Leave a Comment