wp_register_script was called incorrectly

If you set debug to true in wordpress, you might get a notice that wp_register_script or wp_enqueue_script was called incorrectly. The solution to this is to not call these functions directly but wrap them in an action:

Before:

if ( !is_admin() ) {
    wp_deregister_script('jquery');
    wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"), array('jquery'));
    wp_enqueue_script('jquery');
}

After:

if ( !is_admin() ) {
    add_action('wp_enqueue_scripts', 'enqueue_scripts');    
}    

function enqueue_scripts() {
    wp_deregister_script('jquery');
    wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"), array('jquery'));
    wp_enqueue_script('jquery');
}

And the notice is gone!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.