WordPress.org

Ready to get started?Download WordPress

Forums

User Photo
Replicate photo across multisite blogs (18 posts)

  1. kfawcett
    Member
    Posted 3 years ago #

    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?

  2. AARONDAS
    Member
    Posted 3 years ago #

    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

  3. kfawcett
    Member
    Posted 3 years ago #

    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?

  4. jakobneander
    Member
    Posted 3 years ago #

    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.

  5. kfawcett
    Member
    Posted 3 years ago #

    jakobneander,

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

    Thanks!

  6. jakobneander
    Member
    Posted 3 years ago #

    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.

  7. kfawcett
    Member
    Posted 3 years ago #

    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?

  8. jakobneander
    Member
    Posted 3 years ago #

    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. :)

  9. kfawcett
    Member
    Posted 3 years ago #

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

  10. pbernardo
    Member
    Posted 3 years ago #

    Worked for me! thanks

  11. big24fan
    Member
    Posted 3 years ago #

    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?

  12. big24fan
    Member
    Posted 3 years ago #

    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?

  13. Rauli
    Member
    Posted 3 years ago #

    big24fan

    This has the changes recommended by jakobneander

    http://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!

  14. big24fan
    Member
    Posted 3 years ago #

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

  15. lenart
    Member
    Posted 3 years ago #

    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.

  16. lenart
    Member
    Posted 3 years ago #

    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.

  17. lenart
    Member
    Posted 3 years ago #

    After reviewing the code again I noticed the switch_to_blog() call takes quite a lot of time to process. Reading in the WPMU Codex there's a warning not to use this function in front-end. On the regular Codex page there's no warning though.

    I think this problem needs a different solution to my approach above.

  18. bryannielsen
    Member
    Posted 2 years ago #

    I had to modify this plugin for Multisite too but I think my needs were a little different. I wanted all the userphotos in the base directory instead of worrying about the author's primary blog.

    I made two functions to get the base upload directory and the base upload url in a multisite environment -

    function userphoto_get_multisite_upload_dir() {
        return WP_CONTENT_DIR . '/uploads';
    }
    
    function userphoto_get_multisite_upload_url() {
        global $table_prefix, $wpdb;
    
        $base_opt_table = $wpdb->base_prefix . 'options';
        $site_url = $wpdb->get_var("SELECT option_value FROM $base_opt_table WHERE option_name = 'siteurl'");
        $upload_path = $wpdb->get_var("SELECT option_value FROM $base_opt_table WHERE option_name = 'upload_path'"); 
    
        return $site_url . '/' . $upload_path;
    }

    And then plugged that into the appropriate spots in the user-photo.php file.
    Here's a gist of the modified user-photo.php if anybody needs to do the same thing: https://gist.github.com/1443952

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags