Using wp_add_inline_style without a stylesheet

I need to add custom inline styles to the header of a custom theme I’m creating. I’ve come across the wp_add_inline_style() function, which works but doesn’t really suit me as it depends of a specific stylesheet. I’d need to add inline styles at the end of the head tag without a stylesheet dependency.

I’ve tried to set either the theme stylesheet or a non-existent one. In both cases, it works but it’s a bit of a dirty hack IMO (either load the theme stylesheet twice or refer to a ghost file…). Is there a proper way to add inline styles in head without depending of a stylesheet?

Of course, I could add them directly in the header.php file but I’d like to avoid this.


You just need to add the styles directly to the page head. The best way to do this is to use the ‘wp_head’ action hook, assuming you are using a theme that has the hook. Like so:

add_action('wp_head', 'my_custom_styles', 100);

function my_custom_styles()
 echo "<style>*{color: red}</style>";

Check out the WP codex to learn more about action hooks.

Leave a Comment