0

quick question here.

I am adding custom javascript to my wordpress site, i have header-tab.js saved in the child theme's js folder, and tried to call on it with enqueue_script in my child theme's functions.php as shown here:

function includes_header_tab()
{
wp_enqueue_script( 'header-tab', get_template_directory_uri() .'/js/header-tab.js', array( 'jquery' )); 
}
add_action('wp_enqueue_scripts', 'includes_header_tab');
?>

this does not seem to work, can anyone see if there is a problem in my code? or am i missing something? or it might be my javascript file that is at fault?

4
  • Is there an error you're getting on the screen or is it just that your javascript functions aren't firing correctly? Commented Mar 7, 2014 at 16:45
  • Use wp_enqueue_script( 'header-tab', dirname( get_bloginfo('stylesheet_url')).'/js/header-tab.js', array( 'jquery' )); Commented Mar 7, 2014 at 16:50
  • everything on the site loads, but the javascript is just not taking effect as it is supposed to Commented Mar 7, 2014 at 17:02
  • ooh! thank you for telling me about get_bloginfo, now the js loads in the webpage... however it does not seem to take effect and i feel that i need to redo some of my js files Commented Mar 7, 2014 at 17:27

2 Answers 2

1

you are missing a couple of data, first register and then enqueue

 function includes_header_tab()
    {
    wp_register_script( 'header-tab', get_template_directory_uri('/js/header-tab.js', __FILE__ ), array( 'jquery' )); 

    wp_enqueue_script( 'header-tab' );
    }
    add_action('wp_enqueue_scripts', 'includes_header_tab');
    ?>
Sign up to request clarification or add additional context in comments.

Comments

0

As mentioned the get_directory_template_uri manual page:

In the event a child theme is being used, the parent theme directory URI will be returned, get_template_directory_uri() should be used for resources that are not intended to be included in/over-ridden by a child theme. Use get_stylesheet_directory_uri() to include resources that are intended to be included in/over-ridden by the Child Theme.

You simply got the parent's uri and not the child's.

3 Comments

thank you for telling me that, however with function changed, the js still is not taking effect, i think the problem might also be somewhere else
the JS is loaded? try viewing the source and search for a 'header-tab.js' reference
yes the Js is loaded, i see the script tag in the head of the html... so this probably means that the problem lies within my js itself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.