سؤال

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.

هل كانت مفيدة؟

المحلول 3

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.

نصائح أخرى

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

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top