0

I'm trying to add some CSS to the head part of the CSS but I can't get it to work (I want to add it to the head before the Theme CSS so the plugin user can easily over-ride plugins default CSS without !important over-rides). Here is what I tried:

<?php 
add_action('wp_head', 'addCSS');
function addCSS() {
    wp_register_style( 'prefix-style', plugins_url('AddToCartStyles.css', __FILE__) );
    wp_enqueue_style( 'prefix-style' );
}
?>

EDIT: Maybe the way I worded this was confusing. I Don't want to include the CSS in the plugin Admin Page, I want to include the CSS in the regular theme pages (e.g. the pages the end user will actually see).

2
  • See: wordpress.stackexchange.com/questions/21561/…
    – Geert
    Commented May 7, 2012 at 9:04
  • What output do you get? Anything at all? A couple of notes (that may or may not be related): 1) No need to register then enqueue; just put all of the arguments into wp_enqueue_style(), and 2) you should rename your callback. addCSS() is far too generic. Try plugin_slug_enqueue_css() instead. Commented May 7, 2012 at 20:15

2 Answers 2

4

You don't call enqueue or register functions hooking into the wp_head hook. At that hook its too late to add more, I guess, unless you play with priority parameter.

Use the init hook to call wp_enqueue_* & wp_register_* functions.

Do it like this:

<?php
add_action('wp_enqueue_scripts', 'addCSS');
function addCSS() {
    wp_register_style( 'prefix-style', plugins_url('AddToCartStyles.css', __FILE__) );
    wp_enqueue_style( 'prefix-style' );
}
?>

This should work! Just changed the hook in your code.

3
  • 2
    init is not the correct place to enqueue styles and scripts. There is a hook called wp_enqueue_scripts that works for both scripts and styles and only fires on the front end. Commented May 6, 2012 at 22:03
  • @ChristopherDavis You are right! I changed the hook name to wp_enqueue_scripts because its specifically for that purpose but using any hook before that hook is just not using the JIT (just in time) approach, yet it is correct and works fine. Do you mean to say there is more to it? I have always been using init hook to enqueue/register scripts and styles & never ran into any issue. What say?
    – Ashfame
    Commented May 7, 2012 at 8:44
  • wp_enqueue_scripts doesn't work.. I replaced wp_footer with wp_enqueue_scripts and the CSS doesn't show up anywhere in the source code, however with wp_footer as the hook it does show up in the footer.
    – Talon
    Commented May 7, 2012 at 15:03
0

All my plugin pages are something like this my-plugin-base-url-pagerequest

I use something like this:

  // Print Scripts, Styles when My Plugin Pages are loaded.
if (isset($_GET['page']) && substr($_GET['page'], 0, 18) == 'my-plugin-base-url') {

    add_action('admin_print_scripts', 'my-plugin-base-url_load_scripts');
    add_action('admin_print_styles', 'my-plugin-base-url_load_styles');

}

Your Answer

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

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