4

The functions get_bloginfo() and bloginfo() both return information about the blog but accessing it each time seems unnecessary. Is there a function that will return the bloginfo as an object with properties?

More Info:
I have tokens in my markup and I want to replace those tokens with the blog info. Since I'm replacing tokens over multiple posts I don't want to call get_bloginfo("name"); multiple times. Instead I want to put it into an object I can reuse for each call.

4

Taken from the source code of get_bloginfo(), Here is a very very simple class you can utelize and extent at your will.

I have decided to make use of methods, making properties public from a class is really not great coding and not recommended. I know Wordpress thrive on public properties, but that is Wordpress.

Here is the class (which you should convert to make use of proper namespacing, as I said, this is an extremely simple and plain class)

class GetBlogInfo
{
    // Return the home URL
    public function homeURL() 
    {
        return home_url();
    }

    // Return the site URL
    public function siteURL() 
    {
        return site_url();
    }

    // Return the blog description
    public function description() 
    {
        return get_option('blogdescription');
    }

    // Get the feed links
    public function getFeedLink( $link = '' ) 
    {
        switch( $link ) {
            case 'rdf_url':
                $output = 'rdf';
                break;
            case 'rss_url':
                $output = 'rss';
                break;
            case 'rss2_url':
                $output = 'rss2';
                break;
            case 'atom_url':
                $output = 'atom';
                break;
            case 'comments_atom_url':
                $output = 'comments_atom';
                break;
            case 'comments_rss2_url':
                $output = 'comments_rss2';
                break;
            default:
                $output = false;
                break;
        }

        if ( $output ) {
            return get_feed_link( $output );
        } else {
            return false;
        }
    }

    // Return the blog options. Default is name
    public function getOptions( $option = 'name' ) 
    {
        switch( $option ) {
            case 'admin_email':
                $output = 'admin_email';
                break;
            case 'charset':
                $output = 'blog_charset';
                break;
            case 'html_type':
                $output = 'html_type';
                break;
            case 'name':
            default:
                $output = 'blogname';
                break;
        }

        return get_option( $output );
    }

    // Return the blog language setting
    public function language() 
    {
        return str_replace( '_', '-', get_locale() );
    }

    // Return the Wordpress version
    public function version() 
    {
        global $wp_version;
        return $wp_version;
    }

    // Return the pingback URL
    public function pingbackURL() 
    {
        return site_url( 'xmlrpc.php' );
    }

    // Return the path to main stylesheet
    public function stylesheetURL() 
    {
        return get_stylesheet_uri();
    }

    // Return the stylesheet directory uri
    public function stylesheetDirectory() 
    {
        return get_stylesheet_directory_uri();
    }

    // Return the template directory uri
    public function templateDirectory() 
    {
        return get_template_directory_uri();
    }
}

You can use the class as follow:

$q = new GetBlogInfo();
echo $q->homeURL() . '</br>';
echo $q->siteURL() . '</br>';
echo $q->description() . '</br>';
echo $q->getFeedLink( 'rdf_url' ) . '</br>';
echo $q->getFeedLink( 'rss_url' ) . '</br>';
echo $q->getFeedLink( 'rss2_url' ) . '</br>';
echo $q->getFeedLink( 'atom_url' ) . '</br>';
echo $q->getFeedLink( 'comments_atom_url' ) . '</br>';
echo $q->getFeedLink( 'comments_rss2_url' ) . '</br>';
echo $q->getOptions( 'name' ) . '</br>';
echo $q->getOptions( 'admin_email' ) . '</br>';
echo $q->getOptions( 'charset' ) . '</br>';
echo $q->getOptions( 'html_type' ) . '</br>';
echo $q->language() . '</br>';
echo $q->version() . '</br>';
echo $q->pingbackURL() . '</br>';
echo $q->stylesheetURL() . '</br>';
echo $q->stylesheetDirectory() . '</br>';
echo $q->templateDirectory() . '</br>';

This output the following as tested on my test install

http://localhost/wordpress
http://localhost/wordpress
Trots Afrikaans - Praat Afrikaans of hou jou bek!!!
http://localhost/wordpress/feed/rdf/
http://localhost/wordpress/feed/rss/
http://localhost/wordpress/feed/
http://localhost/wordpress/feed/atom/
http://localhost/wordpress/comments/feed/atom/
http://localhost/wordpress/comments/feed/
Pieter Goosen
test@test.com
UTF-8
text/html
af-AF
4.3.1
http://localhost/wordpress/xmlrpc.php
http://localhost/wordpress/wp-content/themes/pietergoosen2014/style.css
http://localhost/wordpress/wp-content/themes/pietergoosen2014
http://localhost/wordpress/wp-content/themes/pietergoosen2014
  • Where would I put this class? Would it be accessible from anywhere? – 1.21 gigawatts Nov 17 '15 at 4:46
  • It can go into a plugin or into functions.php in your theme. I would probably add this in a mu plugin. It will be accessable anywhere no matter where you add it as explained – Pieter Goosen Nov 17 '15 at 5:08
1

These answers are all slower than just using get_bloginfo normally.

Most of the various things that the get_bloginfo function can get use the built in WordPress memory caching system. They don't generally suffer from speed issues from being called multiple times, because things like options and other stuff that come from the database are cached the first time the data is retrieved.

However, calling it a whole bunch of times for some kind of "setup" step like this in advance does make it do a bunch of unnecessary work in querying all that data to start out with, especially if most of that is data you don't actually need to have.

0

Here is my workaround if one does not exist:

/**
 * Get an object with blog info values
 */
function getBlogInfo() {
    $info = new stdClass();

    $info->name                 = get_bloginfo("name");
    $info->description          = get_bloginfo("description");
    $info->wpurl                = get_bloginfo("wpurl");
    $info->url                  = get_bloginfo("url");
    $info->admin_email          = get_bloginfo("admin_email");
    $info->charset              = get_bloginfo("charset");
    $info->version              = get_bloginfo("version");
    $info->html_type            = get_bloginfo("html_type");
    $info->text_direction       = get_bloginfo("text_direction");
    $info->language             = get_bloginfo("language");
    $info->stylesheet_url       = get_bloginfo("stylesheet_url");
    $info->stylesheet_directory = get_bloginfo("stylesheet_directory");
    $info->template_url         = get_bloginfo("template_url");
    $info->template_directory   = get_bloginfo("template_url");
    $info->pingback_url         = get_bloginfo("pingback_url");
    $info->atom_url             = get_bloginfo("atom_url");
    $info->rdf_url              = get_bloginfo("rdf_url");
    $info->rss_url              = get_bloginfo("rss_url");
    $info->rss2_url             = get_bloginfo("rss2_url");
    $info->comments_atom_url    = get_bloginfo("comments_atom_url");
    $info->comments_rss2_url    = get_bloginfo("comments_rss2_url");
    $info->siteurl              = home_url();
    $info->home                 = home_url();

    return $info;
}

// the following is pseudo code to give you example of what I'm doing
$info = getBlogInfo();

for ($i=0;i<count(posts);$i++) {
    $post = $posts[i];
    $value = $post->value.replace("{name}", $info->name);
    $value = $post->value.replace("{description}", $info->description);
    $postsArray.push($post);
}

The reason I chose this as an answer is because I need to access the properties of the object more than once. So once it's created subsequent calls are getting the values not calling the functions repeatedly which may or may not be expensive. I don't know.

Also, the question and answer is not asking "the best" way you can do things it's asking how to do a specific thing and this answer fits that specific thing mentioned in the question. I'm saying all this because people down vote all the time for not doing things the way they were taught or "the best" way.

Update: I added a use case so you can see how I'm using the function and method. I don't always do this but I think it will explain things.

  • 1
    Note that $info->home and $info->siteurl are already given in $info->url and $info->wpurl. These: $info->siteurl = home_url(); and $info->template_directory = get_bloginfo("template_url"); are most likely typos? – birgire Oct 27 '15 at 9:15
  • Not a typo. I added it to match the full API. I know they're the same. – 1.21 gigawatts Oct 27 '15 at 9:19
  • I'm doing performance testing. It takes 23 milliseconds to create that object. It then takes 5 milliseconds to do a search and replace for all the tokens in my HTML. This is on a shared server. Not sure of the specs. – 1.21 gigawatts Oct 27 '15 at 9:20
  • @PieterGoosen I thought there was an info object so I wrote a bunch of code with that in mind. Then I found get_bloginfo returning a string. I have to loop through around ten or more posts so creating an object seems cheaper. I would have to write a test to see. I'm not sure how long it usually takes to serve a page though. I will have to compare it to one of the themes. – 1.21 gigawatts Oct 27 '15 at 9:31
  • 1
    @birgire do have a point here. What really bugs me about this whole concept is the constant repetition of get_bloginfo() and the repition of the values pointed out by birgire. There is really cleaner and better methods to solve this, but having to cope with mobile, I will be pretty useless posting anything close to a solution. As I stated in a (now deleted) comment, this really is something interesting... – Pieter Goosen Oct 27 '15 at 10:11

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.