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.
LVL 7
Robert SaylorSenior DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Robert SaylorSenior DeveloperAuthor Commented:
self solve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.