WordPress Theme & Javascript Bloginfo

Alright, I’m pretty sure this has been done before.

Scenario: I need to access bloginfo() function from inside a script block on my custom theme.

Solution (partial): A partial solution to this is easily remedied with minimal code. However, this solution only allows default bloginfo() to be retrieved.

The following function goes inside your theme’s functions.php file:

function get_the_bloginfo_array()
{
    static $_bloginfo = array( 'name', 'description', 'admin_email', 'url', 'wpurl',
                        'stylesheet_directory', 'stylesheet_url', 'template_directory', 'template_url',
                        'atom_url', 'rss2_url', 'rss_url', 'pingback_url', 'rdf_url', 'comments_atom_url',
                        'comments_rss2_url', 'charset', 'html_type', 'language', 'text_direction', 'version' );
    $jsArray = array( );

    foreach ($_bloginfo as $key)
    {
        $jsArray[$key] = get_bloginfo($key);
    }

    return json_encode($jsArray);
}
var bloginfo = <?php echo get_the_bloginfo_array();?>