ThemeForest

WP_DEBUG notice: wp_enqueue_script was called incorrectly

21 posts
  • Bought between 50 and 99 items
  • Has been a member for 3-4 years
andiszek says

Hi all, i`m going trough the final checks with my theme and there is a WP_Debug notice that`s really bugging me:

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. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in /home/....serverpath…./wp-includes/functions.php on line 2748

the notice is visible in both frontend and backend (with wp_debug turned on).

has anyone else experienced this notice? could you please help with an advice or solution? i can`t seem to find any solution in the codex or on other websites.

is this notice a reason for rejection?

thanks in advance, andi

2932 posts
  • Community Superstar
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • Has been a member for 2-3 years
  • United States
  • Exclusive Author
chrisakelley says

yes its a reason for rejection, how are you enqueuing your scripts?

956 posts
  • Sold between 100 and 1 000 dollars
  • Has been a member for 3-4 years
  • Bought between 10 and 49 items
  • Exclusive Author
  • Microlancer Beta Tester
  • United States
fillerspace says

Maybe you are calling wp_enqueue_script directly from your functions.php file (or another file you included from it). Instead you should create a function that calls wp_enqueue_script and then register it using the wp_enqueue_scripts hook.

function loadMyScripts()
{
    wp_register_script( 'myscript', 'js/myscript.js' );
    wp_enqueue_script( 'myscript' );
}

add_action( 'wp_enqueue_scripts','loadMyScripts' );
2932 posts
  • Community Superstar
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • Has been a member for 2-3 years
  • United States
  • Exclusive Author
chrisakelley says

Maybe you are calling wp_enqueue_script directly from your functions.php file (or another file you included from it). Instead you should create a function that calls wp_enqueue_script and then register it using the wp_enqueue_scripts hook.
function loadMyScripts()
{
    wp_register_script( 'myscript', 'js/myscript.js' );
    wp_enqueue_script( 'myscript' );
}

add_action( 'wp_enqueue_scripts','loadMyScripts' );

It actually looks like he’s using the wrong hook

21 posts
  • Bought between 50 and 99 items
  • Has been a member for 3-4 years
andiszek says

Hi OrganicBeeMedia & Fillerspace, thanks for your replies.

i will try out the function today and will let you know how it works.

until now, i`ve tried to enqueue the scripts in 2 ways:

1. register in functions PHP and enqueue directly in <head>

functions.php

function my_init_method() {
    if (!is_admin()) {

wp_register_script ( // SuperFish
          'superfish',
          get_template_directory_uri() . '/js/nav/superfish/superfish.js',
          array('jquery'), //requires jQuery
          1.48, //Version Nr
          false //loads in footer
        );

    }
}    
add_action('init', 'my_init_method');

----

header.php

<?php wp_enqueue_script( 'superfish' ); ?>

2. enqueue in functions.php (same as above) and add a function to register all scripts”

function register_all_styles()
{

wp_enqueue_script( 'superfish' );

}// end register_all_styles()

add_action('wp_enqueue_scripts','register_all_styles');

i will try out your suggestion to register and enqueue the scripts in the same function.

thanks again, andi

21 posts
  • Bought between 50 and 99 items
  • Has been a member for 3-4 years
andiszek says

Hi all, sorry for the late reply, i had some deadlines :(

i fixed the problem this way (in functions.php):

function my_init_method() {
    if (!is_admin()) {

wp_register_script ( // SuperFish
          'superfish',
          get_template_directory_uri() . '/js/nav/superfish/superfish.js',
          array('jquery'), //requires jQuery
          1.48, //Version Nr
          false //loads in footer
        );

}
}    
add_action('wp_enqueue_scripts', 'my_init_method');

and

function register_all_styles()
{
wp_enqueue_script( 'superfish' ); // Enqueue Superfish Menu
}
add_action('wp_enqueue_scripts','register_all_styles');

hope this will help someone in the future.

thanks, andi

1 post
  • Bought between 1 and 9 items
  • Has been a member for 2-3 years
sunrayshining says

Hi, I’m new to PHP.

Just installed RightNow theme at www.seedposthouse.co.za Theme is ajax based and only works with page navi plugin, but bombs on other kinds of plugins.

Site seems to be working okay, but I get this error when checking: “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. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in /usr/www/users/seedmmpyww/seedposthouse.co.za/wp-includes/functions.php on line 2959 Notice: wp_enqueue_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or init hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in /usr/www/users/seedmmpyww/seedposthouse.co.za/wp-includes/functions.php on line 2959”

Any suggestions most welcome!

I’m guessing that the theme isn’t up to date with wp3.5.1 and needs tweaking, but I’m clueless at this.

Thanks in Advance, W.

by
by
by
by
by