0

I was just wondering if calling get_bloginfo too many times was going to harm performance in a theme and if the data should just be put into its own var?

  • get_bloginfo() returns a global variable if I recall. So calling it repeatedly doesn't query the database multiple times. – Cristian Jul 15 '11 at 19:52
0

get_bloginfo() is not cached and calling it over and over would harm your performance just like creating many calls to the database yourself.

a simple solution which doesn't evolve any caching in to minimize the database and function calls by defining a an array with all the info you need and save that as an option in the wp_options table.

then in your header.php globalize a variable and assign that option to it and every were in your them you can just use that, something like this:

in your functions.php

function set_get_my_blog_info(){
    $my_blog_info = get_option('my_blog_info');
    if ($my_blog_info === false){
        global $wp_version
        $lang = get_locale();
        $lang = str_replace('_', '-', $lang);
        $my_blog_info = array(
            'url' => home_url(),
            'wpurl' => site_url(),
            'description' => get_option('blogdescription'),
            'rdf_url' => get_feed_link('rdf'),
            'rss_url' => get_feed_link('rss'),
            'rss2_url' => get_feed_link('rss2'),
            'atom_url' => get_feed_link('atom'),
            'comments_atom_url' => get_feed_link('comments_atom'),
            'comments_rss2_url' => get_feed_link('comments_rss2'),
            'pingback_url' => get_option('siteurl') .'/xmlrpc.php',
            'stylesheet_url' => get_stylesheet_uri(),
            'stylesheet_directory' => get_stylesheet_directory_uri(),
            'template_url' => get_template_directory_uri(),
            'admin_email' => get_option('admin_email'),
            'html_type' => get_option('html_type'),
            'version' => $wp_version,
            'language' => $lang     
        );
        update_option('my_blog_info',$my_blog_info);
    }
    return $my_blog_info;
}

this will save most of the get_bloginfo options into one option in the database and will only run once.

then in your header.php add

global $my_blog_info;
$my_blog_info = set_get_my_blog_info();

and after you do that you can use that array anywhere you want in your theme, for example instead of:

echo get_bloginfo('url');

simply use your array:

echo $my_blog_options['url'];
4

While there is no inherent caching in get_bloginfo(), most of information it retrieves is from options and get_option() is cached.

Holding value you need in a variable for using several time is still (marginally in most cases) faster than function call, but I wouldn't bother with explicit dedicated caching.

  • Indeed; in fact, it appears that the only get_bloginfo() parameters that don't derive from get_option(), get_site_option(), a global variable, or a constant, are the feed link parameters that derive from get_feed_link(), which dynamically builds the feed link. – Chip Bennett Jul 15 '11 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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