2013-01-09 30 views
7

我想直接從一個WordPress插件插入一串CSS。我不想從外部文件加載它,我不想討論爲什麼這是寫或錯的事情,我只想知道它是否可能。如何在不引用外部CSS文件的情況下在WordPress PHP文件中創建內聯CSS?

換句話說,我知道我能做到這一點:

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

但我不wan't到。

我想要做的是這樣的(僞):

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

我讀wp_register_style的WP抄本(),它似乎並不可能。任何人都有建議?

回答

6

那真是愚蠢的我。幾分鐘後我找到了答案。絕望是一個好動力! :)

的訣竅是不使用wp_register_style()和wp_enqueue_style()

這裏是我做過什麼:

function myStyleSheet() { 

    global $value; 
    $num = $value/2; 

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

不過,也許有更好的辦法?

相關問題