• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 60
  • Last Modified:

Notice: wp_enqueue_style was called incorrectly

Stylesheet in WordPress plugin:

$plugin_dir = "test";
wp_enqueue_style('style', plugins_url("/" . $plugin_dir . "/css/style.css"), array(), null, null);

Open in new window


When this is ran I get a notice warning:

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/username/public_html/wp/wp-includes/functions.php on line 4147

I have spent several hours on google trying different combos but it keeps falling back to this warning.

Note: I am looking for a solution in the plugin folder only. I do not want to mess with the theme. I have another same issue with a script and solving this will also solve another problem I have.
0
Robert Saylor
Asked:
Robert Saylor
  • 2
1 Solution
 
Robert SaylorSenior DeveloperAuthor Commented:
ok, so I solved my question. But I will provide my answer in hopes it helps somebody else.

This will load custom JS and CSS in the plugin and will allow you to pass a variable to the php function.

$plugin_dir = "yourplugindirname";

function grantsearch_theme($plugin_dir) {
    wp_enqueue_script($plugin_dir, plugins_url('/js/ajax.js', __FILE__), array('jquery'), $ver = false, $in_footer = false);
    wp_localize_script($plugin_dir, 'MyAjax', array('ajaxurl' => admin_url('admin-ajax.php')));
    wp_enqueue_style('style', plugins_url("/{$plugin_dir}/css/style.css"), array(), null, null);
}
add_action('wp_enqueue_scripts', function() use ($plugin_dir) {
    grantsearch_theme($plugin_dir);
});

Open in new window


This by it's self is all you need to do. The variable will then pass to the function. You can review developer tools and see the custom JS and CSS loaded. The issue for me every site was giving a final step that was causing an issue.

do_action( 'grantsearch_theme', 'aa', 'bb', 'cc', 'dd' );

Open in new window


However, do not use "do_action" as it will cause this issue:

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/username/public_html/wp/wp-includes/functions.php on line 4147
0
 
Robert SaylorSenior DeveloperAuthor Commented:
self solve
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now