7

I want to enqueue a string of CSS directly from a Wordpress plugin. I don't want to load it from an external file, and I don't want to debate why this is the write or wrong thing to do, I just want to know if it is possible.

In other words, I know I can do this:

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

But I don't wan't to.

What I want to do is something like this (pseudocode):

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

I read the wp codex for wp_register_style() and it doesn't seem possible. Anyone have a suggestion?

1 Answer 1

7

Well that was silly of me. I found the answer a few minutes later. Desperation 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' );

Still, maybe there is a better way?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.