Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a CSS-file generated from PHP.

In my file it says...

header("Content-type: text/css");
define('WP_USE_THEMES', false);
include '../../../../wp-load.php';

I've read that including wp-load.php like this is not good. Why I do this is to get the environment in there.

What is the "correct" way of doing this?

share|improve this question
    
can you post your php generated CSS code? –  Mamaduka Aug 16 '11 at 7:29
1  
You are doubling the server load this way. For every page request, WordPress will be loaded twice. Even after using html cache at server side, WordPress will still be loaded for each CSS request. Bad approach! Instead write the CSS file in the directory as per the conditions and then pick it up as a static resource. –  Ashfame Aug 16 '11 at 8:44

1 Answer 1

up vote 2 down vote accepted

EDIT

Forget my answer below. You could simply use wp_register_style and wp_enqueue_style described in this answer.


You could do it with an additional query variable.

  1. Add a new variable to the $query_vars array with the query_vars filter
  2. On the template_redirect action, make sure your custom.php-css file is included, if the query variable is set.

    // functions.php
    function wpse26013_add_query_vars(query_vars) {
        $query_vars[] = 'style';
        return $query_vars;
    }
    add_filter('query_vars', 'wpse26013_add_query_vars');
    
    function wpse26013_include_custom_css() {
        $style = get_query_var('style');
    
        if($style == 'custom') {
            include_once(get_stylesheet_directory_uri() . '/css/custom.php');
            return;
        }
    }
    add_action('template_redirect', 'wpse26013_include_custom_css');
    

Then you can add a simple css include in your header file.

// header.php
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('stylesheet_url') . '/css/?style=custom'; ?>" />

Last but not least, you have to define your custom css file.

// custom.php
<?php
header('Content-type: text/css');
header("Cache-Control: must-revalidate");
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 1209600) . ' GMT');

$options = get_option('webeo');
?>
#header #logo {
    background: url('<?php echo get_stylesheet_directory_uri() . '/images/' . $options['header']['site_logo']; ?>') no-repeat;
}

You could also get the contents from the css file with the builtin PHP function file_get_contents() and print the css directly to the sourcecode.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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