2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 more
mpc says

Hi guys, I am looking for some nice way to print styles with php variables, most of you use something like Theme Options Framework so you know what I mean (ex: you have a color value stored in the Theme Option Panel and you want to pass to it CSS ). The one way is to just print the styles section into WP header but I guess this is not very nice way… So the other way I found it this http://css-tricks.com/css-variables-with-php/ but for this to work we need to include wp-load.php and because there is problem locating this file I came across a lot of articles saying “DO NOT IMPORT WP -LOAD”. So my question is how do you do this? :)

2457 posts
  • Has been part of the Envato Community for over 5 years
  • Has sold $100+ on Envato Market
  • Has referred 100+ members
  • Has been a beta tester for an Envato feature
+3 more
digitalimpact says

Here’s how I did it in a free theme:

Using the regular style.css for most CSS . Then, for styles that can be set from theme options:

In functions.php:


add_filter('query_vars', 'csp_new_var');
function csp_new_var($public_query_vars) {
    $public_query_vars[] = 'csp_style';
    return $public_query_vars;
}

add_action('template_redirect', 'csp_css_display');
function csp_css_display(){
    $css = get_query_var('csp_style');
    if ($css == 'css'){
        include_once (TEMPLATEPATH . '/cspstyle.php');
        exit;  //This stops WP from loading any further
    }
}

In header.php

<link href="&lt;?php bloginfo('url'); ?&gt;/?csp_style=css" rel="stylesheet" media="all" type="text/css" />

Now, here’s a part of cspstyle.php (I’m using the Option Framework) :


<?php header("Content-type: text/css"); ?>

<?php $bgimage[] = of_get_option('csp_bg_images'); // create an array with the repeating pattern backgrounds
    $imagepath =  get_bloginfo('stylesheet_directory') . '/images/'; // path to theme images folder
    $custombg = of_get_option('csp_custom_background'); // get the custom background value(s)
?>

body{
    color: #e9e2ee;
    font-size: 14px;
    background: <?php if ($custombg) { // user specified custom bg ?> use that! ?>
                    <?php echo $custombg['color']; ?>
                    <?php if ($custombg['image']) {?> url("<?php echo $custombg['image'] ?>");  
                <?php } else { //defaults ?> bgcolor and pixel pattern ?>      

                    <?php echo of_get_option('csp_bgcolor'); ?> 
                    url("<?php echo $imagepath."".$bgimage[0]; ?>.png");
                <?php } } ?>

}

Forum screwed my code a little, but I’m sure you understand.

97 posts
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Made it to the Authors' Hall of Fame
  • Located in Finland
+4 more
Smuliii says

I used this method in my latest theme: WP dynamic stylesheet | ThemeForest Community Forums

3007 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+7 more
duotive says

I really recommend you do not do this. We do this and we will quit it. Just add the css with a function in the theme’s header and not call a php file. It adds support requests and slows the whole theme down.

741 posts
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
Orbital_Themes says
duotive said
I really recommend you do not do this. We do this and we will quit it. Just add the css with a function in the theme’s header and not call a php file. It adds support requests and slows the whole theme down.

Can you elaborate?

3007 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+7 more
duotive says
pogoking said
duotive said
I really recommend you do not do this. We do this and we will quit it. Just add the css with a function in the theme’s header and not call a php file. It adds support requests and slows the whole theme down.
Can you elaborate?

Like what? Ask me please. I do not know what to elaborate more.

214 posts
  • Has referred 1+ members
  • Has collected 50+ items on Envato Market
  • Has been part of the Envato Community for over 5 years
  • Sells items exclusively on Envato Market
agusmu says
duotive said
I really recommend you do not do this. We do this and we will quit it. Just add the css with a function in the theme’s header and not call a php file. It adds support requests and slows the whole theme down.

+1000

we also removed dynamic style php file from our theme last month because of performance issue, you can use tools.pingdom.com to test the load time and compare it…

adding styles directly to your header using wp_head hook is better option for faster website load time.

741 posts
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
Orbital_Themes says

What are the support requests about? What kind of problems do your buyers have with this method? And regarding slowing down – you mean that it takes too much time to load WP manually or too many database requests to get every single option?

3007 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+7 more
duotive says
pogoking said
What are the support requests about? What kind of problems do your buyers have with this method? And regarding slowing down – you mean that it takes too much time to load WP manually or too many database requests to get every single option?
Buyes have issues with server configurations. If folders leading to public_html have 777 permission set the dinamic php stylesheets will not work and give a 500 internal server error. For the slow down, most customers have a shared host and shared hosts do not really like having requests to php files after the website draw has finished. I mean the server outputs the dom and then it needs to execute another php file. It slows them down a lot.
2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 more
mpc says

So it is better to place the “custom” css in the Header with add_action(‘wp_head’, ‘add_my_head’); ?

by
by
by
by
by
by