An email from wordpress!
Today I uploaded v1.0.1 of garland-revisited to the wordpress site, a few minutes later i got an email from wordpress.org!
Trying to pull in wp-config.php or wp-load.php directly is a bad idea. The actual location of those files can be in a nearly infinite number of locations, so trying to reference them directly is guaranteed to break in at least some of the cases.
The code in question is in css.php, the file is requested out of wordpress to provide the custom css, but css.php has to use the wordpress database to find out your custom colours etc..
[php]require( ‘../../../wp-load.php’ );[/php]
The code itself works with a default install of wordpress, but a few people out there might define a different folder for wp-content/themes.. so after som hefty google-fu I found this http://planetozh.com/blog/2008/07/what-plugin-coders-must-know-about-wordpress-26/
[php]$rootPath = dirname(__FILE__);
$rootPathArray = explode("/",$rootPath);
$numDirs = count($rootPathArray);
for ($i=1; $i<=$numDirs; $i++) {
$path = implode("/", $rootPathArray);
if (file_exists($path.’/wp-load.php’)) {
require_once($path.’/wp-load.php’);
break;
} elseif (file_exists($path.’/wp-config.php’)) {
require_once($path.’/wp-config.php’);
break;
}
array_pop($rootPathArray);
}[/php]
This snippet walks back through the dirs until it hits either wp-load or wp-config, I have tested it on a local copy it works fine, so the next version will inlude the fix.
EDIT:
I forgot to follow this post up woth the actual fix from the wordpress guys 😉
css.php is the file i want to call from outside wordpress, but it needs to be wp-config.php loaded first, so the following code is added to the functions.php:
[php]add_filter(‘query_vars’, ‘add_my_var’);
function add_my_var($public_query_vars) {
$public_query_vars[] = ‘garland_css’;
return $public_query_vars;
}
add_action(‘template_redirect’, ‘my_var_output’);
function my_var_output() {
$myvalue=get_query_var(‘garland_css’);
if ($myvalue) {
include(‘css.php’);
exit; // this stops WordPress entirely
}
}[/php]
now i can call the css file from a wordpress url like:
[code]http://myblogurl.com/index.php?garland_css=1[/code]
I got another email from the wordpress folks today, with a code suggestion to fix this little problem.
The next release will include this new code, it works perfectly!