I am including a particular PHP file as a style sheet using

<link href="<? echo THEME_CSS;?>/styles.php" media="screen" rel="stylesheet" type="text/css">

Recently I reached the point where I needed to use TEMPLATEPATH and various built-in WordPress functions. Alas I am getting

"Warning: require_once(TEMPLATEPATH/framework/theme.php) [function.require-once]: " and similar errors indicating that this file is not taking advantage of the WordPress environment in general.

If WordPress was even working correctly here, TEMPLATEPATH should be the actual template path. In addition, other functions such as get_bloginfo(), etc.. are not found.

Is there someway I can initialize WordPress explicitly to let me pull options from the admin panels?

3 Answers 3


If you're linking to a file, it's opened as a new file. So unless you explicitly include wp_load.php or something within that styles file, WordPress is not available.

Loading WP all over again in one page load seems like complete overkill to me. Why don't you just include your style file as an inline stylesheet, if you need to use WP engine?

Do something like this:

<?php include_once('styles.php'); ?>
  • Well basically this is just because a large amount of inline styles seems like poor code in my book. Your dissent of wp_load clued me in to what I'm looking for however. How bad of overkill is this really? It seems to load very swiftly and I would think much of it would be cached from the first load. Untrue? At least now require_once for wp-blog-header.php works great for making headway if I can't find a more elegant solution. Will consider inlining at the end. Commented Apr 20, 2011 at 4:25
  • 1
    Yeah, it depends on your environment; with a good caching setup, you might not be adding too much overhead. On some shared hosting setups, though, that could be adding a half second or more to every page load. Make whatever tradeoff makes sense for you between efficiency and elegant markup. If its only a few variables that are changing in the stylesheet, you might be able to inline those after linking to the rest of the stylesheet... Commented Apr 20, 2011 at 6:08

You may consider using some alternative functions to return the path you are looking for instead, for example:

//this one may return as well as some unwatned theme-specific stuff

//this returns the pure url


//basically the same as template_directory, may come over with extra info

//returns the url followed by style.css

These very well could be subject to the same problem, let me know if this doesn't work. You may have to throw in some includes at the top of your file regardless of what you end up using in order to get any of these to work.

  • Actually this is the first thing I tried. Unfortunately what I'm saying is that all the core functionality seems inaccessible. i,e.. in this case I get Fatal error: Call to undefined function get_bloginfo() Commented Apr 20, 2011 at 2:41
  • 1
    Dammit! Alrighty, were using the file before anything else is parsed basically... This one is a little heavy for me and from my experience chasing this stuff down is pretty tedious. What is confusing me is that if you're calling the file from within your template, I wouldn't think it should behave this way. Can you be more specific on how and when you are using this file. Commented Apr 20, 2011 at 2:53
  • Yeah it is rather confusing. I'm using the call to <link> after the rest of my header, the very last thing before </head>. Before this there are plenty of calls to WP functions and environment variables. I'm thinking it must be because I am using a php based stylesheet but I have certainly done this before. Is there some way to explicitly initialize WordPress? Commented Apr 20, 2011 at 3:46

The concept of PHP-driven style sheets is sub-optimal because the PHP interpreter - and the WordPress bootstrap, which is fat! - has to be started for every CSS request.

If possible, consider using relative paths - note that if your style sheet and the image are in the same template directory, things like


will work! Paths in style sheets are relative to the actual style sheet's location, not the page's.

Alternatively, while inline styles are indeed bad, consider having a static style sheet, and specifying only the dynamic properties in the document head.

<link rel="stylesheet" href="....." type="text/css">
<style type="text/css">
 .class { background-image: url(<?php echo $xyz; ?>) }
  • The thinking seems very sound but I have a great many options.How could I rewrite the static stylesheet every time the options change? Would it be as simple as just adding a file saving function onto the "Save Changes" submit? Commented Apr 20, 2011 at 21:37
  • 1
    @Garet are you really sure you need this in the first place? Are your CSS and image files not in the same template directory to start with, making it possible to use relative paths? ... Or do you have other dynamic settings as well?
    – Pekka
    Commented Apr 20, 2011 at 21:40
  • @Pekka: images are no problem, I've got colors, borders, placement..all sorts. Commented Apr 20, 2011 at 21:42
  • 1
    Garet I see. Are they dynamic per user? Where does the dynamic data come from?
    – Pekka
    Commented Apr 20, 2011 at 21:44
  • 1
    @Garet hmmm. How about writing the dynamic data into a CSS static file, and adding the PHP code that does that to the correct action or hook?
    – Pekka
    Commented Apr 20, 2011 at 21:51

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.