How can I create inline CSS in a WordPress PHP file without linking to an external CSS file?

I want to insert a CSS string directly from a Wordpress plugin. I don’t want to download it from an external file, and I don’t want to discuss why this is being written or something is wrong, I just want to know if this is possible.

In other words, I know I can do this:

wp_register_style('myStyleSheet', 'my-stylesheet.php'); wp_enqueue_style( 'myStyleSheet'); 

But I do not want to do this.

I want to do something like this (pseudo code):

 $number = get_option('some_width')/2; $css = " .divbox { width: $number; } "; wp_register_style('myStyleSheet', $css); wp_enqueue_style( 'myStyleSheet'); 

I read wp codex for wp_register_style () and this seems impossible. Anyone have a suggestion?

+7
source share
1 answer

Well, that was stupid with me. A few minutes later I found the answer. Despair is a good motivator! :)

The trick is not to use wp_register_style () and wp_enqueue_style ()

Here is what I did:

 function myStyleSheet() { global $value; $num = $value/2; echo ' <style type="text/css"> .divbox { width: '.$num.'; } </style> '; } add_action( 'wp_print_styles', 'myStyleSheet' ); 

However, maybe there is a better way?

+6
source

All Articles