ThemeShaper Forums » Thematic

[closed]

wp_enqueue_script called incorrectly (Wordpress 3.3 Beta)

(6 posts)
  • Started 2 years ago by caseysays
  • Latest reply from middlesister
  • This topic is not resolved
  1. caseysays
    Member

    Unfamiliar with bbPress forums so forgive me if this has been answered elsewhere; I couldn't find an answer.

    Fresh install of Thematic on Wordpress 3.3Beta2 gave the following error:

    "Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or init hooks."

    Following the instructions at http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    I replaced "wp_enqueue_script('jquery');" on line 80 of the functions.php with the following:

    function my_scripts_method() {
    // register your script location, dependencies and version
    wp_register_script('custom_script',
    get_template_directory_uri() . '/js/custom_script.js',
    array('jquery'),
    '1.0' );
    // enqueue the script
    wp_enqueue_script('custom_script');
    }
    add_action('wp_enqueue_scripts', 'my_scripts_method');

    I'm new to Thematic and I just wanted to confirm with someone that this will work without messing anything up down the road.

    Posted 2 years ago #
  2. you removed the script that enqueues jquery?

    Posted 2 years ago #
  3. sergiovieira
    Member

    This fixed it for me, thanks!

    Posted 2 years ago #
  4. maybe you can't call wp_enqueue_scripts() directly any more

    i'm surprised that this works b/c your custom-script won't even load w/o jquery (since the array('jquery') means that jquery is a dependency)..

    btw- you don't need to register and then enqueue.... enqueue will auto-register something that isn't already registered

    function my_scripts_method() {
    
    //enqueue jquery
    wp_enqueue_script('jquery');
    
    // enqueue the script
    wp_enqueue_script('custom_script',
    get_template_directory_uri() . '/js/custom_script.js',
    array('jquery'),
    '1.0' );
    }
    add_action('wp_enqueue_scripts', 'my_scripts_method');
    Posted 2 years ago #
  5. middlesister
    Member

    Actually, there is no need to enqueue jquery separately. Declaring it as a dependency will make wordpress include it, that is what the parameter is for. If I understand correctly, you could even declare a dependency like array( 'jquery', 'jquery-ui-draggable', 'jquery-ui-sortable' ) and wordpress will include them before your script. caseysays code looks like it is taken straight from the codex, so it should work without problems.

    Regarding calling wp_enqueue_script() directly, well you can but it is not recommended. I think they just added the notice in the latest wordpress version to notify theme authors about that. In general, it is a good idea to have all code added to some kind of hook and not run in the main body of the file. This gives you more control over when things are run and in which order, and will avoid conflicts and errors in other cases (such as trying to call custom taxonomies or use conditionals before they are set up).

    Maybe a bug should be filed for this.

    Posted 2 years ago #
  6. middlesister
    Member

    I just realized the OP was using the code in thematic's functions.php and not in a child theme.

    @caysays: To remove the notice, simply replace wp_enqueue_script('jquery'); with

    function thematic_enqueue_scripts() {
        wp_enqueue_script('jquery');
    }
    add_action('wp_enqueue_scripts', 'thematic_enqueue_scripts');
    Posted 2 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.