[wp-hackers] siteurl and home variables / constants

Joost de Valk joost at yoast.com
Mon Mar 30 08:29:01 GMT 2009


Hey,

I was looking at the code for site_url in 
/wp-includes/link-template.php, and saw that it /always/ does a 
get_option('siteurl'), even when, as on my site, WP_SITEURL is defined 
in wp-config.php. The same goes for get_bloginfo in 
/wp-includes/general-template.php, for both WP_SITEURL and WP_HOME.

Wouldn't it be good to check for that before doing the get_option, to 
save a query or two on each page load?

It would be quite easy to make those functions use check for the defined 
constants before doing a database call.

A quick plugin I cooked[1] up that does this by using the pre_option_* 
hook, showed me that this would save quite a number of calls (though of 
course after the two first calls, these would be cached). The only 
"issue" I encountered was that in functions.php's is_blog_installed 
function, a db query is done to check whether a blog is installed, and 
the query that's done is, you've guessed it, retrieving the siteurl...

Anyway, I think some performance improvement could be made by adding in 
a check for the defined constants, but maybe I'm missing something?

Best,
Joost


[1] The source for that plugin is very simple:

function checkwp_config_siteurl() {
     if ( defined( 'WP_SITEURL' ) ) {
         // error_log("Prevented get_option");
         return WP_SITEURL;
     }
     return false;
}
add_filter('pre_option_wpurl','checkwp_config_siteurl',10,1);

function checkwp_config_home() {
     if ( defined( 'WP_HOME' ) ) {
         // error_log("Prevented get_option");
         return WP_HOME;
     }
     return false;
}
add_filter('pre_option_url','checkwp_config_home',10,1);
add_filter('pre_option_siteurl','checkwp_config_home',10,1);
add_filter('pre_option_home','checkwp_config_home',10,1);


me *Joost de Valk*
Online Marketing, WordPress, SEO & Social Media Strategy
OrangeValley <http://www.orangevalley.nl> & Yoast <http://yoast.com>
E: joost at orangevalley.nl <mailto:joost at orangevalley.nl> - 
joost at yoast.com <mailto:joost at yoast.com>
T: +316-24-555-808 | @yoast <http://twitter.com/yoast> on Twitter



More information about the wp-hackers mailing list