0

I've been making a find-a-dealer type plugin in WordPress and I finally got it all done, everything works on my local and dev server.

I push it up to production and upon activation, I get these errors/warnings:

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. Please see Debugging in WordPress for more information. (This message was added in version 3.3.0.) in /home/sites/my_site/public_html/wp-includes/functions.php on line 4773

Notice: wp_register_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.0.) in /home/sites/my_site/public_html/wp-includes/functions.php on line 4773

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.0.) in /home/sites/my_site/public_html/wp-includes/functions.php on line 4773

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.0.) in /home/sites/my_site/public_html/wp-includes/functions.php on line 4773

Notice: Undefined offset: 0 in /home/sites/my_site/public_html/wp-includes/plugin.php on line 914

Notice: Undefined offset: 0 in /home/sites/my_site/public_html/wp-includes/plugin.php on line 933

Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /home/sites/my_site/public_html/wp-includes/class-wp-hook.php on line 286

Looking at these errors, I immediately went to my plugin's code for handling registering css/js:

main-plugin-file.php (skimmed down)

function fad_css_js()
{
    # add css
    wp_register_style('front_css', plugins_url('skin/css/front.css', __FILE__));

    # register css
    wp_enqueue_style('front_css');
}

# actions
add_action('init', 'fad_css_js');

admin-plugin-file.php (skimmed down)

function adn_css_js()
{
    # css
    wp_register_style('adn-css', plugins_url('skin/css/admin.css', __FILE__));
    wp_enqueue_style('adn-css');

    # js
    wp_register_script('adn-js', plugins_url('skin/js/admin.js', __FILE__), array(), false, true);
    wp_enqueue_script('adn-js');
}

add_action('admin_enqueue_scripts', adn_css_js());

Like I said, this worked on my localhost and development server. Going to production seems to bring up all these errors, it (the code) doesn't look to be in the wrong, I register before any enqueue, so I'm really not sure.

Going to the debugging page on WP site wasn't helpful..

How do I debug/resolve this?

Edit

Can confirm that a different plugin with the css/js code like this:

function my_other_js_css()
{
    # add css
    wp_register_style('my_other__css', plugins_url('css/main.css', __FILE__));
    wp_register_style('my_other_media_css', plugins_url('css/media.css', __FILE__));

    # add js
    wp_register_script('my_other_js', plugins_url('js/main.js', __FILE__), array(), false, true);

    # register css
    wp_enqueue_style('my_other_css');
    wp_enqueue_style('my_other_media_css');

    # register js
    wp_enqueue_script('my_other_js');
}

add_action('plugins_loaded', array('the_plugin', 'get_instance'));
add_action('init', 'my_other_js_css');
  • note that these are still problems on your local machine, even if you don't see the warnings. You shouldn't enqueue things on the init hook or others. There are only a handful of valid hooks to register and enqueue on, and they're all mentioned in the warning – Tom J Nowell May 31 at 15:57
  • @TomJNowell huh, weird considering I have errors enabled o.O but OK.. so what should it be? I followed the WP guide to plugin dev a while back.. so I'm guessing they've updated procedures since then? also weird that enabling the plugin on local + dev doesn't have the headers already sent thing whereas production did.. but I guess that's WP being WP xD – treyBake May 31 at 15:59
  • it sounds like your PHP was configured to show only errors, not warnings ( warnings != errors ). Preferably you would have it set to log all errors to a PHP error log that you could then comb through – Tom J Nowell May 31 at 16:00
0

The warning is self-explanatory:

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.

You have to call wp_register_style on the correct hooks:

  • wp_enqueue_scripts for the frontend
  • admin_enqueue_scripts for the backend
  • login_enqueue_scripts for the login page

Calling wp_register_script on any other hook, or no hook at all, is incorrect, and will generate that warning. This is because unless you call it on those 3 hooks, the functions might not work as expected, and your script might not be enqueued or registered. Either because it's registered too late, or, because it's registered too early

Also, you can register and enqueue at the same time by using wp_enqueue_script and passing all the parameters. wp_register_script can be useful, but it isn't always necessary


As for why it appeared in production and not locally, these are PHP warnings, and your local environment may be configured to only show errors, not warnings/notices. I recommend logging everything to an error log and checking that. A plugin such as query monitor can also aid in revealing warnings and notices.

  • see my answer :) – treyBake Jun 3 at 12:03
0

Here is where the error lied:

add_action('admin_enqueue_scripts', adn_css_js());

the adn_css_js() needed to be 'adn_css_js':

add_action('admin_enqueue_scripts', 'adn_css_js');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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