Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm loading some javascript files in the parent theme. The path in the parent theme is "scripts" > "custom.js".

In the child theme, I'm creating the same path (scripts > custom.js) and changing some of the jQuery inside the custom.js file.

The problem is that the changes are not being applied. Is this the wrong way to make changes to these files in the child theme?

share|improve this question
1  
What is the parent Theme? How are the scripts being called by the parent Theme? –  Chip Bennett Aug 24 '11 at 22:17

1 Answer 1

up vote 14 down vote accepted

Child themes only override files in the main theme directory (like header.php) that are called in with functions like get_template_part or get_header, etc.

The correct way to add scripts to WordPress is with wp_enqueue_script. If your parent theme uses this, you can override the JS files by using wp_dequeue_script and enqueuing your own.

Like so...

<?php
add_action( 'wp_enqueue_scripts', 'wpse26822_script_fix' );
function wpse26822_script_fix()
{
    wp_dequeue_script( 'parent_theme_script_handle' );
    wp_enqueue_script( 'child_theme_script_handle', get_stylesheet_directory_uri() . '/scripts/yourjs.js', array( 'jquery' ) );
}

If the parent theme isn't using wp_enqueue_script, it's probably hooking into wp_head (or wp_footer) to echo out the scripts there. So you'd use remove_action to get rid of those functions echoing the scripts out, and then enqueue your own script.

If the script is hard coded into the template file, you'll just need to replace that template file in your child theme without the script tag.

If they used wp_enqueue_script calls that utilize get_stylesheet_directory_uri, then you shouldn't have to do anything. Since this isn't happening, you'll just have to poke around and see what the theme author did.

share|improve this answer
2  
if the parent theme uses get_stylesheet_directory_uri to enqueue scripts, then the child theme will have to duplicate all scripts thus enqueued, because otherwise they woun't be found. There is no fallback mechanism in get_stylesheet_directory_uri to check if an individual file exists in the child theme and fall back to the parent theme's file if necessary. –  user18579 Jul 26 '12 at 13:20
    
From the Codex: “wp_print_scripts should not be used to enqueue styles or scripts on the front page. Use wp_enqueue_scripts instead. ” codex.wordpress.org/Plugin_API/Action_Reference/… –  Christian Lescuyer Aug 13 '13 at 14:53
    
Keep in mind when this was written: two years ago. Before wp_enqueue_scripts could be used to only enqueue scripts on the front end. Updated. If you see something out of date, feel free to edit. –  chrisguitarguy Aug 13 '13 at 15:02
    
Sorry, no criticism implied, I did upvote your answer :) –  Christian Lescuyer Aug 16 '13 at 19:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.