2

I'm at my wit's end here... here is the code I use to try and get a CSS file to work in my WordPress plugin:

add_action('wp_enqueue_scripts', 'initial_admin_links_hide_stylesheet');

function initial_admin_links_hide_stylesheet() {
    wp_enqueue_style( 'prefix-style', plugins_url('initial_hide_admin_links.css', __FILE__));   
}

Then I call the function by using this line of code:

initial_admin_links_hide_stylesheet();

If I comment out the call to the function I receive no notices. If I leave it uncommented, the notice I receive is:

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 /home2/jakereva/public_html/wp-includes/functions.php on line 3547

As far as I can tell, the code has been written correctly, but I absolutely cannot get that notice to go away when I call the function. Help! Thanks in advance.

2 Answers 2

4
add_action('wp_enqueue_scripts', 'initial_admin_links_hide_stylesheet');

This tells Wordpress to hook the script into the process for you. There is no need to call the function manually.

Make sure you have wp_head() and wp_footer() calls in your theme files where appropriate or it want be added

/**
 * Proper way to enqueue scripts and styles
 */
function theme_name_scripts() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
Sign up to request clarification or add additional context in comments.

3 Comments

If I take out the function call (initial_admin_links_hide_stylesheet();), the CSS never displays. If I leave it in, the CSS executes, but I get the notice. In my own header.php file, I do have a wp_head() call.
Two things here. 1) You also have to have wp_footer() call in your footer. 2) Not that important but I try to always do the add_action after I declare the function.
I'm using the parent theme's footer.php file, so there's a call to the footer in there. Changing the position of the add_action doesn't make a difference.
0

Change hook name to wp_head. This will enqueue style in header.

Check the WordPress action reference to see what actions being called. https://codex.wordpress.org/Plugin_API/Action_Reference

In front you can use wp_head. In admin pages: admin_enqueue_scripts

2 Comments

The notice goes away, but the CSS never gets called/loaded
Check the WordPress action reference to see what actions being called. codex.wordpress.org/Plugin_API/Action_Reference

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.