I have script in my functions.php that seeks to locate the media directory of the site in which the theme is installed. This is pretty simple unless the site is an MU site. In that case, the media directory is based on the blog_id.

However, my code below is returning the main site id rather than the blog_id of the site in which its being run…

function get_image_list()
{
    global $current_site;
    $dir=is_multisite() ?  'wp-content/blogs.dir/'.$current_site->blog_id.'/files/' : 'wp-content/uploads/';
    $url=is_multisite() ?  get_bloginfo('url').'/wp-content/blogs.dir/'.$current_site->blog_id.'/files/' : get_bloginfo('url').'/wp-content/uploads/';

In this case, the actual blog_id is 3, however, its returning a value of 1 for $current_site->blog_id

The error is…

cannot open wp-content/blogs.dir/1/files/

2 Answers
2

Compare $current_site->id with $current_site->blog_id. According to the in-line documentation, blog_id should be working … but check to see if there’s a major difference there (your system might have a plug-in or something else causing a problem).


Update – Ignore last

It seems like $current_site is a global variable defined by your site or network and will always return the same blog_id as your network dashboard – in this case, “1.”

What you need to use instead is $current_blog:

function get_image_list() {
    global $current_blog;
    $dir=is_multisite() ?  'wp-content/blogs.dir/'.$current_blog->blog_id.'/files/' : 'wp-content/uploads/';
    $url=is_multisite() ?  get_bloginfo('url').'/wp-content/blogs.dir/'.$current_blog->blog_id.'/files/' : get_bloginfo('url').'/wp-content/uploads/';

That should get you the right information.

Leave a Reply

Your email address will not be published. Required fields are marked *