[wp-hackers] wp-config.php, wp-load.php, and ABSPATH

Otto otto at ottodestruct.com
Tue Sep 15 14:07:53 UTC 2009


Wow. Everybody else got that one wrong too. :)

The right way is to not load "styles.css.php" directly in the first
place. If you need to load the WordPress files, then you should load
WordPress itself, and direct it to do what you want it to do.

If you want to make some kind of output outside the normal "flow" of
WordPress, but need the WordPress functions to do it:

First, realize that WordPress is going to load twice anyway. The goal
is not to eliminate this (if you need wpdb access in your separate
file, then you sorta have to do load WP again), but to eliminate the
need to know the WordPress file paths and such.

Example code:
function plugin_query_vars($public_query_vars) {
	$public_query_vars[] = 'SOME_IDENTIFIER';
	return $public_query_vars;
}
function plugin_special_output() {
	if(intval(get_query_var('SOME_IDENTIFIER')) == 1) {
		// do whatever output you like here
		exit; // stop normal WordPress execution
	}
}
add_filter('query_vars', 'plugin_query_vars');
add_action('template_redirect', 'plugin_special_output');


After you add this code, a call to any normal WordPress URL with
?SOME_IDENTIFIER=1 stuck on the end will return your code instead.
That code can be *anything*. If you want to output CSS, output CSS.
Change the headers if you like. Whatever.


-Otto


More information about the wp-hackers mailing list