WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Notice: wp_deregister_script was called incorrectly (4 posts)

  1. Peter Wooster
    Member
    Posted 2 years ago #

    This notice which is new in WP 3.3 is caused by the common practice in themes and plugins of using wp_register_script in open code within a php file such as functions.php. This code needs to be run in the wp_include_scripts action which is currently not documented in the Plugin API. If your theme or plugin includes code like:

    wp_deregister_script('jquery');
    wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"), false, '1.7.1');
    wp_enqueue_script('jquery');

    it now needs to be something like:

    function mytheme_enqueue_scripts() {
           wp_deregister_script('jquery');
           wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"), false, '1.7.1');
           wp_enqueue_script('jquery');
    }
    add_action('wp_enqueue_scripts', 'mytheme_enqueue_scripts');

    Plugin and theme authors should fix this immediately as it's undecipherable to average users given that the message does not include the module and line that actually caused the problem.

    The wp_register_scripts action is documented in script_loader.php:

    * Wrapper for do_action('wp_enqueue_scripts')
     *
     * Allows plugins to queue scripts for the front end using wp_enqueue_script().
     * Runs first in wp_head() where all is_home(), is_page(), etc. functions are available.
     *
     * @since 2.8

    Wouldn't it be nice if WP just threw exceptions when it detects a Notice or higher error. If you can't figure out where the offender is, try adding this inside the doing_it_wrong function in wp_includes/functions.php (currently at line 3585):

    throw new Exception("Testing");

    This will cause a stack trace to be printed and your site to die at the error. After that it's easy to find and fix.

    I hope this helps.
    /peter

  2. kitchin
    Member
    Posted 2 years ago #

    Thanks very much for posting this. Note if the affected page is on the admin side, it needs this instead:

    add_action( 'admin_enqueue_scripts', ... )

    The debug notice gives no clue where the bad code started, somewhere in wp-content. I like your "throw" way, so I added a a note about it it to my simple debug traceback code: http://pastebin.com/va542PMw which can be run on a live site, and without modding wp-includes (since there are hooks).

    WP_DEBUG almost always brings up notices of bad and deprecated code, in my experience. The top one is probably has_cap, where people are using numbers in add_submenu_page() and the like. I always have to remember to put in a capability, not a role. So change '10' to 'activate_plugins' not 'Administrator'. It's a two step process looking up the docs through User_Level_to_Role_Conversion and then Capability_vs._Role_Table on the same page.

  3. kitchin
    Member
    Posted 2 years ago #

    Also the place to queue styles has been changing. Now the recommendation is one way for themes, and another way for plugins: Function_Reference/wp_enqueue_style

  4. hafman
    Member
    Posted 1 year ago #

    Thanks for this, i just got a stalled js slider working in Magazinium with this info. Much obliged.

Topic Closed

This topic has been closed to new replies.

About this Topic