0

this is the header section for enqueueing my scripts

<?php 
wp_enqueue_script( 'menu' );
wp_enqueue_script('thumbnail');
?>
<?php wp_head(); ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>"  type="text/css"  />
<script type="text/javascript" src="<?php get_template_directory_uri();?>/js/menu-effect.js"></script>
<script type="text/javascript" src="<?php get_template_directory_uri();?>/js/thumbnail-effect.js"></script>
</head>

and this is functions.php section

function my_jsfile() {
wp_register_script( 'menu', get_template_directory_uri() . '/js/menu-effect.js', array() );
wp_register_script( 'thumbnail', get_template_directory_uri().'/js/thumbnail-effect.js', array() );
}
add_action( 'wp_enqueue_scripts', 'my_jsfile' );

2 Answers 2

1

You should enqueue scripts in functions.php itself with wp_enqueue_scripts hook. Like this.

function my_jsfile() {

  wp_register_script( 'menu', get_template_directory_uri() . '/js/menu-effect.js', array() );
  wp_register_script( 'thumbnail', get_template_directory_uri() . '/js/thumbnail-effect.js', array() );

  wp_enqueue_script( 'menu' );
  wp_enqueue_script( 'thumbnail' );

}
add_action( 'wp_enqueue_scripts', 'my_jsfile' );

And also you should remove wp_enqueue_script from header.php as well as script links.

And finally make sure your script paths are correct.

3
  • still not working..i suppose its something in the header which is causing problem.. May 30, 2015 at 4:05
  • Did you remove wp_enqueue_script from header.php? And made sure path of these JS files are correct.
    – Robert hue
    May 30, 2015 at 4:08
  • yes ..i removed script codes from header.php and the paths are correct as well.. May 30, 2015 at 4:15
0

wp_enqueue_scripts fires after the call to the function wp_head(). So your wp_enqueue_script() calls are too early.

Register your scripts on wp_loaded, enqueue them on wp_enqueue_scripts.

add_action( 'wp_loaded', function() {
    $base = get_template_directory_uri();
    wp_register_script( 'menu',      "$base/js/menu-effect.js" );
    wp_register_script( 'thumbnail', "$base/js/thumbnail-effect.js" );
});


add_action( 'wp_enqueue_scripts', function() {
    wp_enqueue_script( 'menu' );
    wp_enqueue_script('thumbnail');
});
2

Your Answer

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

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