How to synchronize user profile fields in multisite?

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 );
}

Leave a Comment