• Is there any way to have the photos replicate across all of the blogs within a network?

    Basically when a user signs up at any one of the blogs in my WP Multisite network, they are registered across all of them. The problem I’m having is that User Photo uploads images under the blog directory that the user updates his profile in, even though the user only has one. So I need a way to place the image in the other blogs within the network.

    Any ideas?

Viewing 15 replies - 1 through 15 (of 17 total)
  • For MU, it is going to create a folder, so under your blogs.dir it will create a folder called 1 or 2 or 3 for each site,and then it will create the userphoto folder, so without massive recoding you won’t be able to access the same user photo across multiple sites.

    thanks,

    aaron

    Thread Starter kfawcett

    (@kfawcett)

    So how about a php script that checks those user photo folders and if it finds a new photo it copies it to all of the other sites user photo folders?

    I have run in to a similar problem, and my solution was to hack the plugin slightly. Basically I added switch_to_blog calls to the functions in the plugin that retrieve and store the image, so it looks in the correct folder every time.

    Thread Starter kfawcett

    (@kfawcett)

    jakobneander,

    Would you mind posting the section of the userphoto plugin that you edited?

    Thanks!

    Sure

    function userphoto__get_userphoto($user_id, $photoSize, $before, $after, $attributes, $default_src){
    	global $authordata;
    	if (function_exists('is_multisite') && is_multisite()) {
    	   	switch_to_blog($authordata->primary_blog);
        }
    function userphoto_profile_update($userID){
    	global $userphoto_validtypes;
    	global $current_user;
    
    	$userdata = get_userdata($userID);
    	if (function_exists('is_multisite') && is_multisite()) {
    	   	switch_to_blog($userdata->primary_blog);
        }
    function userphoto_display_selector_fieldset(){
    
        #NOTE: an email needs to be sent to the admin when a contributor uploads a photo
    
        global $profileuser;
        global $current_user;
        if (function_exists('is_multisite') && is_multisite()) {
    	   	switch_to_blog($profileuser->primary_blog);
        }

    And then for good measure a

    if (function_exists('is_multisite') && is_multisite()) {
    		restore_current_blog();
        }

    In the bottom of each of those functions.

    Just be warned, I have only tested the functions I’m using myself so I’m not sure all functionality of the plugin is covered by these changes, but at least it should give you an idea of how to do it.

    Thread Starter kfawcett

    (@kfawcett)

    I’m no php expert, so forgive my questions?

    What exactly is the switch_to_blog doing? I see that it is pointing to primary_blog. What does primary_blog do/mean?

    switch_to_blog takes the blog id provided, and sets up the WordPress environment so that is the active blog, i.e. uses correct uploadsdir in blogs.dir and database etc.

    primary_blog is a value in each users user_meta with the blog id that is his/hers primary blog.

    So what it does is it looks at the user, switches WordPress over to that users blog, does whatever it needs to do with the photo and then restores_current_blog() which switches is back to how it was before.

    Not sure if it is the most elegant or safe way to do it, but I needed a quick hack and this worked for me. ??

    Thread Starter kfawcett

    (@kfawcett)

    Thank you very much! I will see if it works for me.

    Worked for me! thanks

    Please forgive me, but I am a newbie at this. I am having the original problem mentioned on this page. I have read jakobneander’s solution and want to try it, however, I am not very sure how to include it in the PHP file. Do I need to replace the current functions with these functions? When it says add the last bit to the bottom of the functions, where do they go? Can I see an example of the full user-photo.php page or something?

    Please forgive me, but I am a newbie at this. I am having the original problem mentioned on this page. I have read jakobneander’s solution and want to try it, however, I am not very sure how to include it in the PHP file. Do I need to replace the current functions with these functions? When it says add the last bit to the bottom of the functions, where do they go? Can I see an example of the full user-photo.php page or something?

    big24fan

    This has the changes recommended by jakobneander

    https://rauliweb.com/user-photo.php.txt

    copy and paste the text into an empty text document and save as user-photo.php using notepad or something similar then rename the old user-photo.php file in its plugins folder, then upload to the same folder and test it.

    If it don’t work your theme might interefere with it, as I discovered with the Easel theme I am using on one project.

    Enjoy it while its up!

    That worked! Thank You! I will have to compare the 2 and see exactly what he meant to try. Thanks again!

    I use WP network to show the same site in different languages. I wanted to use the same code on my site but it didn’t work. There are some problems with the hack mentioned above!

    Because I was uploading photos for all members they were stored under the blog I was logged in to (let’s say 3). The user’s preference might be different (let’s say 2) so this won’t work as it will try to get the photo from blog 2 instead of blog 3.

    One way to solve this would be to force uploading to the same folder every time (blog 1) since it always exists. For this we’d need to use something else instead of wp_upload_dir(); which returns the upload directory for currently active blog.

    Another way would be to save the blog ID in user_meta (when uploading the photo) and use it when we want to show the picture. This might be the quickest and safest way.

    I hope I’ll be able to fix this and upload the updated code.

    Rather than using above code I changed the plugin so that it writes the blog ID from which the photo was uploaded and use this when retrieving the photo. I think it’s better and works even if administrators change users photos.

    Here’s the idea:

    if (function_exists('is_multisite') && is_multisite()) {
    $blog_with_photo = get_user_meta($userID, 'userphoto_blog_id', true);
    if ( !empty($blog_with_photo) ) {
    switch_to_blog($blog_with_photo);
    } else {
    add_user_meta($userID, 'userphoto_blog_id', $blog_id, true);
    // no need to switch blog as we'll use this blog as user's default
    }
    }

    And link to working file:
    https://gist.github.com/1021388#file_user_photo.php

    Feel free to update the code (gist) and make it even better.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘[Plugin: User Photo] Replicate photo across multisite blogs’ is closed to new replies.