0

I've got a multisite install in development where both the subdomains and the main site use the exact same page templates for showing data. However, when I try to get a profile image (added ID in wp_usermeta), I get false on the subdomains, but the actual image data returned on the main domain.

I came across that it might be a permalink issue, but both have the same structure. Just to make sure I changed, saved, changed back, saved them. Also figured that it 'could' be due to entering a string, so casted the ID to int as well, just to be safe.

Anyone know what might be causing this?

/* Code */
if( isset( $user[ 'meta' ][ 'prof-pic' ] )){
    $logo = wp_get_attachment_image_src( $user[ 'meta' ][ 'prof-pic' ][ 0 ], 'pic' );
    echo '<pre>';
    var_dump($user[ 'meta' ][ 'prof-pic' ][ 0 ]);
    var_dump($logo);
    var_dump(wp_get_attachment_image_src( (int)$user[ 'meta' ][ 'prof-pic' ][ 0 ], 'pic' ));
    echo '</pre>';
}

/* Results Subdomain */
string '94' (length=2)
boolean false
boolean false

/* Results Main site */
string '94' (length=2)
array (size=4)
  0 => string 'http://site/wp-content/uploads/2014/06/Viper-160x100.jpg' (length=90)
  1 => int 160
  2 => int 100
  3 => boolean true
array (size=4)
  0 => string 'http://site/wp-content/uploads/2014/06/Viper-160x100.jpg' (length=90)
  1 => int 160
  2 => int 100
  3 => boolean true

2 Answers 2

1

wp_get_attachment_image_src() takes ID of attachment "post type" (in quotes because it's native WP post type so not quite same thing as CPT).

Now while users are shared in multisite, posts are not. The ID (94) that points to specific attachment in your main site has no meaning whatsoever in any other site of your multisite.

At which point it boils down to mucking with getting data from different site. It might be easier approach to store actual URL to image in user metadata as well.

1
  • Thank you for the quick answer. I will start on a workaround for that and post that as well. I might not be the only one bumping into this.
    – rkeet
    Jul 2, 2014 at 12:18
0

Based on @Rarst his answer, I've created the following workaround.

An additional field created_on should be added to a users' metadata upon creation. This is to identify the subdomain the user was created on and where the profile image might reside. So add this to your code ;)

Place this function in a file where you get your user data or the functions.php of your theme.

/**
 * Gets the profile image of a customer or regular user (editor/participant)
 * @param $user_id      - id of the user to get the image for
 * @param $image_name   - name of the image-size to return
 * @return array|bool   - returns image source data, or false
 */
function getUserProfileImageSrc( $user_id, $image_name ){
    $profile_image_id = get_user_meta( $user_id, 'prof-pic', true );   //image of profile picture id
    $created_on = get_user_meta( $user_id, 'created_on', true );   //id of blog where the user was created
    $created_here = ($created_on == get_current_blog_id() ) ? true : false;   //check if the user was created on the current site
    $image_id = $profile_image_id ? $profile_image_id : false;  //check if $profile_image_id exists, otherwise false
    if( $created_here ){
        return wp_get_attachment_image_src( $image_id, $image_name );   //Image created on current site, get & return image
    } elseif( !$created_here ){
        $current_blog = get_current_blog_id();  //get current blog id
        switch_to_blog( $created_on );  //switch to blog to query correct tables
        $img = wp_get_attachment_image_src( $image_id, $image_name );  //get image
        switch_to_blog( $current_blog );   //switch back to current blog
        return $img;
    }
    return false;             //something didn't work, return false
}

Addition: A field, created_on, needs to get added with the id of the blog the user is created on. Like so:

//creating user
//lots of fancyness
update_user_meta( $new_user_id, 'created_on', get_current_blog_id() );
//finish creating user

So. Hopefully this will help someone out in the future. Thanks again to @Rarst for pointing out that an image gets uploaded as a WP Post Type, therefore making it bound to a blog/subdomain and not an ID!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.