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


Tags: php,css,wordpress

Problem :

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?



Solution :

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?


    CSS Howto..

    How do I integrate a jQuery script in to my html file?

    how to create Hollow triangle in css [duplicate]

    How do I make this type of layout with CSS? [closed]

    ckeditor - how to get custom-css with id to work?

    How do I remove the underline from an image wrapped in an anchor?

    CSS selectors - how to select 'for' in CSS?

    How can I incorporate a slideshow into HTML?

    How can I display an alert with the position of the element that was clicked?

    How do align images vertically centers with CSS and HTML?

    How to make a css navigation menu “selected” option still clickable

    How to get element by its partial class name CSS

    how to transform in both X & Y direction with CSS Only [duplicate]

    how to make certain css 3d effect works in IE

    How to align multiple input using css

    How to change css styles of a jQuery plugin?

    How to modify css class of container for specific element ID

    how to give css height a percentage of a different div (above original div)

    Getting a resolution error on Chrome and Firefox for C9 app. Not sure how to resolve

    How to set a background-color to element which has the same id with anchor of url?

    How change the radio button hover color

    How to add hover effect to img placed in parent container

    960 grid css : How do I change vertical padding between divs?

    How to load css and js

    jQuery addClass based on how far scrolled

    how to overwrite inline styles in print css?

    how to put html inside php code [closed]

    How to blend an image with a solid colour in CSS?

    how to combine multiple css class selectors into one?

    How to override the slide effect to dissolve effect in JQuery Mobile using CSS3 feautures?

    How to make one file compatible with different browsers?