wp_get_attachment_image_src multisite issue

I’m running the latest version of WordPress 4.4.1, and I’m having a very frustrating issue. I run a multisite installation, and use Advanced Custom Fields throughout the site to allow the imagery to be content manageable through the admin panel.

On every site, except the primary site, imagery isn’t working if not hardcoded. For example, on each page I allow the user to upload a banner which will show at the top of each page.

For example, if they’re on site #2, when they upload imagery it’s placed into:

/wp-content/uploads/sites/2/YEAR/MONTH/

However, whatever method I use to try and display these images in the templates, it always gets the URL wrong, excluding the /sites/siteID/ part.

I first thought this was an issue with the plugin Advanced Custom Fields, and the way it was trying to output the URLs. I’ve ruled this out now, as I setup Advanced Custom Fields to pass out an attachment URL, and then I used wp_get_attachment_image_src to convert the attachment ID into a URL. This also doesn’t work, so now I’m really stuck!

Here’s the multisite part in my wp-config.php:

/* Multisite */
define('WP_ALLOW_MULTISITE', true);
define('SUNRISE', 'on');
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true);
define('PATH_CURRENT_SITE', "https://wordpress.stackexchange.com/");
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

I’ve tried commenting out the last three lines there, thinking it might just be confusing WordPress into thinking every site on the network was the first, primary site. This didn’t help.

1 Answer
1

Some of the more archaic code is still present in WordPress, notably globalising variables inside functions. A good example of thise is get_current_blog_id(), which is present in wp-load.php:

/**
 * Retrieve the current blog ID.
 *
 * @since 3.1.0
 *
 * @global int $blog_id
 *
 * @return int Blog id
 */
function get_current_blog_id() {
        global $blog_id;
        return absint($blog_id);
}

This means that you could potentially run into issues if you set $blog_id yourself in your theme. This goes for $current_site and some other variables too.

By the sounds of your questions I’d guess you’re building a theme or plugin yourself, so there’s a good chance you’re using the above variables. Good practice is to prefix your variables with something unique to your theme.

Leave a Comment