4

I'm using wordpress multisite. I installed Multisite user management plugin.

If a user register in my main sites then that user automatically added in all my sites.

My problem it doesn't copy the profile datas. I don't want my users to fill the same data in all 50 sites. So is there any code available to sync the datas?

I mean if a user fill the profile fields in main sites all sub sites should be updated with the same value. Likewise if the user edit the profile in site10.example.com it should be synced and updated in all sites. Is it possible?

Thanks

1
  • I'd try contacting the author of the plugin to see if they can add this functionality.
    – shea
    Commented Oct 27, 2012 at 4:51

1 Answer 1

4

This code works, but it's only been tested in a localhost environment.

There's a function extracted from the plugin Multisite User Management, which is based on the get_blog_list function, that has been deprecated.

No alternative available. For performance reasons this function is not recommended.

The user meta has to be checked individually, i.e.: Aim, Jabber, etc.

add_action( 'admin_init', 'wpse_38421_init');

function wpse_38421_init() 
{
    add_action( 'personal_options_update', 'wpse_38421_save_profile_fields' );
    add_action( 'edit_user_profile_update', 'wpse_38421_save_profile_fields' );
}

function wpse_38421_save_profile_fields( $user_id ) 
{
    
    $user_url = ( isset( $_POST['url'] ) && '' !== $_POST['url'] ) 
                ? $_POST['url'] : false;
                
    $user_aim = ( isset( $_POST['aim'] ) && '' !== $_POST['aim'] ) 
                ? $_POST['aim'] : false;
                
    $user_yim = ( isset( $_POST['yim'] ) && '' !== $_POST['yim'] ) 
                ? $_POST['yim'] : false;
                
    $user_jabber = ( isset( $_POST['jabber'] ) && '' !== $_POST['jabber'] ) 
                ? $_POST['jabber'] : false;

    $current_site = get_current_blog_id();
    $all_blogs = wpse_38421_get_blog_list( 0, 'all' );

    foreach ( $all_blogs as $key => $blog ) 
    { 
        if ( 
            is_user_member_of_blog( $user_id, $blog[ 'blog_id' ] ) 
            && $current_site != $blog[ 'blog_id' ] 
            )
            continue;

        switch_to_blog( $blog[ 'blog_id' ] );

        if ( $user_url ) 
            update_usermeta( $user_id, 'url', $user_url );
            
        if ( $user_aim ) 
            update_usermeta( $user_id, 'aim', $user_aim );
            
        if ( $user_yim ) 
            update_usermeta( $user_id, 'yim', $user_yim );
            
        if ( $user_jabber ) 
            update_usermeta( $user_id, 'jabber', $user_jabber );
    }
    
    switch_to_blog( $current_site );
}

/**
 * Based on the deprecated WPMU get_blog_list function. 
 * 
 * Except this function gets all blogs, even if they are marked as mature and private.
 *
 * Extracted from the plugin http://wordpress.org/extend/plugins/multisite-user-management/
 */
function wpse_38421_get_blog_list( $start = 0, $num = 10 ) {
    global $wpdb;

    $blogs = $wpdb->get_results( $wpdb->prepare( 
            "SELECT blog_id, domain, path FROM $wpdb->blogs 
            WHERE site_id = %d AND archived = '0' AND spam = '0' AND deleted = '0' 
            ORDER BY registered DESC", $wpdb->siteid 
            ), ARRAY_A );

    foreach ( (array) $blogs as $details ) {
        $blog_list[ $details[ 'blog_id' ] ] = $details;
        $blog_list[ $details[ 'blog_id' ] ]['postcount'] = $wpdb->get_var( 
                "SELECT COUNT(ID) FROM " 
                . $wpdb->get_blog_prefix( $details['blog_id'] )
                . "posts WHERE post_status='publish' AND post_type='post'" 
                );
    }
    unset( $blogs );
    $blogs = $blog_list;

    if ( false == is_array( $blogs ) )
        return array();

    if ( $num == 'all' )
        return array_slice( $blogs, $start, count( $blogs ) );
    else
        return array_slice( $blogs, $start, $num );
}

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.