Wednesday 12 February 2014

How to Enqueue javascript/jQuery in theme footer with wp_enqueue_script

You can simple enqueue theme .js files in the wp_enqueue_script function in functions.php but what if you want to enque a block of javascript code or jQuery code in the footer of the theme.

One solution is to hard code the code in the footer by including opening and closing tags of script but if your are a theme designer you can have error by your theme reviewer or theme check plugin that one hard coded javascript or jQuery block of code found in footer.php the the following solution will work perfectly

go to your favorite editor and create a new custom-functions.js file paste the block of code and save the file with extension .js this will create a new jQuery file (custom-functions.js) in your theme /js directory now you can simply enqueue the  .js file with wp_enqueue_script function.


/*register and enque theme scripts and styles*/ 
add_action( 'wp_enqueue_scripts', 'oopthemes_scripts_styles' );
function oopthemes_scripts_styles() {
    // Custom jQuery Functions
    wp_enqueue_script( 'custom-functions', get_template_directory_uri() . '/js/custom-functions.js', array(), '1.0', true );
}

Share and Comment your idea or problem regarding wp_enqueue_script

1 comment :

  1. Here are some of the most feature rich and best health beauty wordpress themes . Choose one and get started with your website now.

    ReplyDelete