0

I'd like to include a functionality in a theme that will overwrite styles from previously included CSS files. For this purpose, I need to be able to display a <style> in the header after ALL stylesheets have been displayed.

Is there a hook to do this?

2 Answers 2

2

Hook to wp_enqueue_scripts with a high priority number. For example:

function load_my_style_wpse_90225() {
  wp_register_style( 'mystyle', get_template_directory_uri() .'/path/to/stylesheet.css');
  wp_enqueue_style( 'mystyle' );
}
add_action('wp_enqueue_scripts','load_my_style_wpse_90225',1000);

That should reasonably load last in line.

Basically the same answer as here: Wooslider custom css

Refernce

http://codex.wordpress.org/Function_Reference/add_action

1
  • This is exactly what I was looking for. Wasn't aware that the priority parameters exists for actions too.
    – Dzhuneyt
    Commented Mar 13, 2013 at 14:08
0

Although it is generally not recommended, we can hook to the [wp_print_styles][1] action, which displays after wp_head, wp_enqueue_scripts, and wp_enqueue_styles.

So (in functions.php):

function my_theme_custom_css_handler(){
    echo '<style>custom css here</style>';
}
add_action('wp_print_styles', 'my_theme_custom_css_handler');

For more information and a full listing of actions, you can check the Action Reference in the WordPress Codex.

3
  • Creating a new style sheet and calling it last in the queue would be the proper way to do this. Else you could just add the styles to the header directly. I don't see the point of this either way. Commented Mar 10, 2013 at 18:23
  • Linking to styles directly in the header is not recommended by the WordPress Codex and is a common reason for rejecting a theme from approval in the Theme Repository.
    – Dzhuneyt
    Commented Mar 10, 2013 at 21:42
  • Yes but you were already going to use a method that was not recommended. What is the difference? Beside when I said directly I meant with style tags <style></style> Commented Mar 10, 2013 at 21:48

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.