WP_DEBUG notice: wp_enqueue_script was called incorrectly

Hi all,
im going trough the final checks with my theme and there is a WP_Debug notice thats 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

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

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' );
1 Like
fillerspace said

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

1 Like

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
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' ); ?>

  1. 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

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

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

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.

I am having same message issue with visual composer

I am using admin-print-styles for my scripts, and it got soft rejected. I am confused.

add_action( ‘admin_print_styles-’ . $this->page, array( &$this, ‘_enqueue’ ) );

bah, added 4 lines of code to replace 1 hook. Bah.

Work for me!