• Resolved ktfmc

    (@ktfmc)


    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!!!

Viewing 1 replies (of 1 total)
  • Hi there, I can’t say for certain without looking at everything in context, but I think you may be doing your updates the long way around. The reason I’m thinking this is that user meta on a multisite isn’t stored in a different place for each site. It’s all in a single table, wp_usermeta, which is shared by all the sites. So if you add or update the meta for one, it should by its nature be synchronized across all sites.

    The only time you’d need to get the blogs of the user in question is if you were storing user meta that was going to be different for each blog — which is the opposite of what you’re describing. And even then you wouldn’t need to use switch_to_blog() unless you’re storing user-related information in a specific table for that blog, which doesn’t appear to be the case here.

    All your code should need to do when a custom user profile field is updated is:

    1. Check that the data exists and is valid (for instance, check that an email field contains a properly-formatted email)

    2. Use add_user_meta() if the field is new or update_user_meta() if it’s just an update to existing data. This will update the entry in wp_usermeta, which in turn should populate all your sites with new info.

    I don’t know if this will help you at all, but you may want to look at https://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields and see if it’s useful.

Viewing 1 replies (of 1 total)
  • The topic ‘multisite 2-way user profile sync’ is closed to new replies.