33

I don't know how but now I am getting this error :

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

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

Warning: Cannot modify header information - headers already sent by (output started at wp-includes/functions.php:3049) in wp-login.php on line 415

Warning: Cannot modify header information - headers already sent by (output started at wp-includes/functions.php:3049) in wp-login.php on line 427

This is what I get when I try to log in... On other sites, I have only two first errors. I try to fix it but I can't. Any suggestions?

2
  • 1
    The Notices seem pretty self explanatory. What have your tried and what didn't it work?
    – s_ha_dum
    Commented Mar 6, 2014 at 20:06
  • @s_ha_dum I have tried many things what I have found on Google... just now toscho's solutions worked for me, and I can login to my admin panel, but what I can see is that something goes wrong with loading style, because font is not the same as it was. I keep getting this errors:
    – raiden
    Commented Mar 6, 2014 at 20:35

7 Answers 7

19

Disable all plugins and switch to the default theme. It should be gone now.

Then enable each addon step by step, until the problem comes back. You know the source now, let’s say a plugin.

The plugin calls probably wp_enqueue_script too early. Find all occurrences of that function, then make sure they are bound to specific actions:

  • wp_register_script() should be called for the action wp_loaded
  • wp_enqueue_script on one of the actions
    • wp_enqueue_scripts,
    • admin_enqueue_scripts,
    • customize_controls_enqueue_scripts or
    • login_enqueue_scripts (see this thread for the latter).
3
  • The problem is that I can't log in becuase I also get this weird error with cookies. Error says, that I have blocked cookies in my browser, but I don't have blocked them... It's so weird because all what I have done today it was adding a few lines to style.css, everything was good, but a few hours after I had those errors.
    – raiden
    Commented Mar 6, 2014 at 20:12
  • @raiden Then rename your plugin directory per FTP, do the same for the currently active theme.
    – fuxia
    Commented Mar 6, 2014 at 20:13
  • Now I can login to my panel... thank you! but now I get one error more Warning: Cannot modify header information - headers already sent by (output started at wp-includes/functions.php:3049) in wp-includes/pluggable.php on line 896
    – raiden
    Commented Mar 6, 2014 at 20:20
17

Based on Gregory Schultz's solution:

Wrap all your scripts and styles in a function and hook that function to your target enqueue action. 👇

function my_admin_scripts() {
  wp_enqueue_style( 'admin-css', get_stylesheet_directory_uri() . '/admin/css/admin.css' );
  wp_enqueue_script( 'admin-js', get_stylesheet_directory_uri() . '/admin/js/admin.js', true );
}
add_action( 'admin_enqueue_scripts', 'my_admin_scripts' );

wp_enqueue_scripts — front-end

admin_enqueue_scripts — admin page

login_enqueue_scripts — login page

1
  • 1
    This surely helps, thank you :)
    – jrran90
    Commented Nov 29, 2020 at 14:29
4

Rather than disable all your plugins one by one, or modify your codebase to debug, you can use the Query Monitor plugin to narrow down the source of this error.

When you see this error reported in the console:

wp_register_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.0.)

The last column (Component) will tell you the plugin that is triggering the error. From there do a search inside of that plugin (or theme) to find and patch the issue.

3

The reason why this is happening is because these functions are being called straight from the functions.php file, without using a function. Find out which lines in functions.php aren't inside a function like this, and add them.

Example for datepicker:

-Instead of this in functions.php:

wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-ui-css', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css');
wp_enqueue_style('jquery-ui-css', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');

-We add this

// We define the function:
function MYTHEME_scripts() {
    wp_enqueue_script('jquery-ui-datepicker');
}
    
// Add the functions to WP loading list.
add_action( 'wp_enqueue_scripts', 'MYTHEME_scripts' );
    
function MYTHEME_styles() {
    wp_enqueue_style('jquery-ui-css', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css');
    wp_enqueue_style('jquery-ui-css', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
}
    
// Add the functions to WP loading list.
add_action( 'wp_enqueue_scripts', 'MYTHEME_styles' );
1
  • 1
    There is no hook wp_enqueue_style. Best and cleanest solution, when problem appears on the website, not the admin, is from @Usman Ahmed
    – Ludwig
    Commented May 5, 2021 at 22:24
1

It means there are 3 "endpoints" for wp_enqueue_script() which are wp_enqueue_scripts for the frontend, login_enqueue_scripts for the login screen, admin_enqueue_scripts for the admin dashboard. Check this link.

You get this error because wp_enqueue_script() was called unproperly.

4
  • I understand what's there, but I don't know how to (implement?) this "The fix: Use wp_enqueue_scripts instead."
    – raiden
    Commented Mar 6, 2014 at 20:26
  • it means you just have to use wp_enqueue_scripts instead of wp_print_styles but I doubt it's related to your case. It's just an example of troubleshooting. In your case there are some functions in plugins or theme that misuses wp_enqueue_script() so you get this error... my guess.
    – JMau
    Commented Mar 6, 2014 at 20:37
  • I was getting Notice: wp_register_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. I wrapped all my scripts and styles in a function and the notice went away. Commented May 24, 2018 at 0:24
  • Besides that, I get no errors Commented May 24, 2018 at 0:25
1

Wrap it in a hook called wp_enqueue_scripts.

Example:

add_action( 'wp_enqueue_scripts', function() {
    wp_enqueue_style('style', get_stylesheet_uri(), false, false, 'all');
    wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/script.js', true );
});
0

What resolved this issue for me was downloading WordPress classic editor and classic widgets plugin

1
  • 1
    That sounds like it's solved this by turning off the broken code though, not actually fixing it
    – Rup
    Commented Dec 17, 2021 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.