X

WP enqeue script

How to use the wp_enqueue_script in a correct way.

In your header file call the wp_enqueue_script before wp_head, like this:

Code Snippet:

<?php wp_enqueue_script( 'jquery' ); ?></p>
<p><?php wp_head(); ?>

And then put all your javascript code after wp_head.

You can give this function another name but don’t use “my_init_method” because this will conflict with other WP functions.

The only thing you have to do now is to call your javascript using the no-conflict mode. This means that you can’t call the script using the $ shortcode but like this:

Code Snippet:

jQuery(document).ready(function({<br />
// stuff<br />
});

Code Snippet:

<?php
function your_init_yourscript() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');
}
add_action('init', 'your_init_yourscript');
?>

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!