multisite 2-way user profile sync
-
Let me start with bear with me. I run non-profit and try to save every penny I can so we can give to charity at te end of the year. I am NOT a programmer, designer, or anything of that nature. I don’t think I have had a class in such an area since elementary and earned DOS. I basically sit and figure it out. After a plug-in demolished my site though “I’ll just try it” is out the window!
I have several sites in my multisite network. I want 3 of those sites to sync user meta data if updated on any of those 3 sites. I have MANY customized user fields. I am not using Buddypress as it is not as front-end user friendly and back-end customizable as Ultimate Member. After some research here is the code I have put together (I am reducing this code to just 3 of the fields out of the 40 I have in reality for purpose of this post. I am trying to figure out first if there is something that I am sure many may see as obviously wrong and 2) if I am supposed to put this in my wp-settings file or somewhere else.
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_farm = ( isset( $_POST['farm'] ) && '' !== $_POST['farm'] ) ? $_POST['farm'] : false; $user_position = ( isset( $_POST['position'] ) && '' !== $_POST['position'] ) ? $_POST['position'] : false; $current_site = get_current_blog_id(); $all_blogs = get_blogs_of_user($user_id, $all = false ); foreach ( $all_blogs as $key => $blog ) { if ( is_user_member_of_blog( $user_id, $blog[ 'blog_id' ] ) && $current_site != $blog[ 'blog_id' ] ) function get_blogs_of_user( $user_id, $all = false ) { global $wpdb; $user_id = (int) $user_id; // Logged out users can't have blogs if ( empty( $user_id ) ) return array(); $keys = get_user_meta( $user_id ); if ( empty( $keys ) ) return array(); if ( ! is_multisite() ) { $blog_id = get_current_blog_id(); $blogs = array( $blog_id => new stdClass ); $blogs[ $blog_id ]->userblog_id = $blog_id; $blogs[ $blog_id ]->blogname = get_option('blogname'); $blogs[ $blog_id ]->domain = ''; $blogs[ $blog_id ]->path = ''; $blogs[ $blog_id ]->site_id = 1; $blogs[ $blog_id ]->siteurl = get_option('siteurl'); $blogs[ $blog_id ]->archived = 0; $blogs[ $blog_id ]->spam = 0; $blogs[ $blog_id ]->deleted = 0; return $blogs; } $blogs = array(); if ( isset( $keys[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) { $blog = get_blog_details( 1 ); if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) { $blogs[ 1 ] = (object) array( 'userblog_id' => 1, 'blogname' => $blog->blogname, 'domain' => $blog->domain, 'path' => $blog->path, 'site_id' => $blog->site_id, 'siteurl' => $blog->siteurl, 'archived' => 0, 'spam' => 0, 'deleted' => 0 ); } unset( $keys[ $wpdb->base_prefix . 'capabilities' ] ); } $keys = array_keys( $keys ); foreach ( $keys as $key ) { if ( 'capabilities' !== substr( $key, -12 ) ) continue; if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) continue; $blog_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key ); if ( ! is_numeric( $blog_id ) ) continue; $blog_id = (int) $blog_id; $blog = get_blog_details( $blog_id ); if ( $blog && isset( $blog->domain ) && ( $all || ( ! $blog->archived && ! $blog->spam && ! $blog->deleted ) ) ) { $blogs[ $blog_id ] = (object) array( 'userblog_id' => $blog_id, 'blogname' => $blog->blogname, 'domain' => $blog->domain, 'path' => $blog->path, 'site_id' => $blog->site_id, 'siteurl' => $blog->siteurl, 'archived' => 0, 'spam' => 0, 'deleted' => 0 ); } } /** * Filter the list of blogs a user belongs to. * * @since MU * * @param array $blogs An array of blog objects belonging to the user. * @param int $user_id User ID. * @param bool $all Whether the returned blogs array should contain all blogs, including * those marked 'deleted', 'archived', or 'spam'. Default false. */ return apply_filters( 'get_blogs_of_user', $blogs, $user_id, $all ); } continue; switch_to_blog( $blog[ 'blog_id' ] ); if ( $user_url ) update_usermeta( $user_id, 'url', $user_url ); if ( $user_farm ) update_usermeta( $user_id, 'farm', $user_farm ); if ( $user_position ) update_usermeta( $user_id, 'position', $user_position ); } switch_to_blog( $current_site ); }
Thank you for any help!!!
- The topic ‘multisite 2-way user profile sync’ is closed to new replies.