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');
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');
Share
Improve this question
edited Jun 3, 2019 at 11:45
treyBake
asked May 31, 2019 at 15:17
treyBaketreyBake
12712 bronze badges
3
|
2 Answers
Reset to default 0The 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 frontendadmin_enqueue_scripts
for the backendlogin_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.
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');
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745441515a4627852.html
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 ♦ Commented May 31, 2019 at 15:57