0

I am including a .css which i parse with php like so

<link rel="stylesheet" type="text/css" media="all" href="<?php echo get_childTheme_url(); ?>/css/productgallery.php" />

Now in productgallery i can not use a global variable (an array) which i have set in functions.php

How do i fix this?

1
  • ok i almost got this one solved by adding `define('WP_USE_THEMES', false); require('../../../../../site/wp-blog-header.php');' to the top of productgallery.php i can echo the array i need to use, but the productgallery is a mess so the wordpress engine is triggering more then i need.
    – alex
    Commented Mar 4, 2012 at 14:32

3 Answers 3

5

Short answer, no.

Long Answer

In your comment, you explain that you're including wp-blog-header.php in the PHP file that generates your CSS file. This is a very bad idea.

When your page references your stylesheet, your browser sees the reference and makes a separate HTTP request to your server to pull it down. If the stylesheet is a static file, this isn't a problem. It's usually cached (so the browser loads it locally) or can be served from a CDN (meaning repeated requests never hit your server).

But loading it as a dynamic PHP file means it's not cache-able. Including wp-blog-header.php also means you're loading all of WordPress just to generate that file.

Every request to your site loads WordPress not once, but twice just to generate that one file.

What you should do instead

Extract the dynamic parts of your stylesheet. Then, in your header.php file, include an in-line <style type="text/css"> ... </style> block.

It won't be the cleanest thing in the world, but inside header.php you can use whatever PHP variables you need to generate the CSS. And you external stylesheet stays small, static, and cache-able.

2
  • Tx for the long answer explanation. Your def. right about that one. No need to load all WP. I am going to try your suggestion.
    – alex
    Commented Mar 4, 2012 at 16:23
  • Great explanation, just what I needed to know! Thanks. Commented Apr 8, 2012 at 16:14
2

You should be adding stylesheets using the wp_enqueue_style() function inherent to wordpress, this can be called any time after init and before wp_print_styles from anywhere in wordpress (functions.php, plugin file, even a template in some cases).

1
  • @mOr7if3r, i am aware of that. But in my case i need to generate some dynamic div reference for slider use.
    – alex
    Commented Mar 4, 2012 at 16:23
0

If you're working with PHP, there's no reason you can't apply conditional classes to an element. For example

<?php $some_other_class ="bobby";?>
<div class='some_class <?php echo $some_other_class;?>">Some stuff</div>

And then the css:

.bobby {
some css stuff here
}

What this will end up looking like in the HTML is:

<div class='some_class bobby'>

Which essentially appends another css selector to the element. Of course keep in mind the hierarchy, and how css is applied in a cascade. This means any css applied via this mentho is applied after the first class. Of course this also means you can use the second class to over-ride the initial styling, useful if you want to add color, change fonts or whatever conditionally.

Of course the css itself would reside in the stylesheet.

I don't recommend including inline style essentially anywhere, as @EAMann mentioned. The reason being, it just makes troubleshooting that much harder.

I can think of VERY few instances where you'd need to include inline styling in the header like he suggests, and even then, only in circumstances where doing it inside the template as I suggested isn't possible. Worst case, you can always call css style-sheets conditionally through wordpresses enqueue/register function, which negates pretty much any reason to throw it in the header like he suggests.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.