Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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).

share|improve this question
    
    
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. –  Chip Bennett May 7 '12 at 20:15
add comment

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
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. –  chrisguitarguy May 6 '12 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 May 7 '12 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 May 7 '12 at 15:03
add comment

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');

}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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