current user ID different on different sites

Hi there,

Hope you can please help, or advise...

With some help my purchase user sync plugin is doing as it should and syncing users. However the ID for each user is different on each website. I'm guessing this is normal, however if i want to collect "current user" date from my database. My 2nd (slave) website uses the same database (but uses a different table prefix).

My aim is to assign data from website 2 using "$current_user". Is there a way i can achieve this with something other than $current_user->ID to access data when website 2 uses prefix2_mytables (e.g. prefix2_users) and website 1 uses prefix1_mytables (e.g. prefix1_users)?

Maybe too general a question for this forum, but wondering if possible and what the best method might be.

Initially i'd looked at sharing the same user and usermeta tables for both sites (e.g. , but with this manual approach i can't copy across roles.

Any thoughts would be greatly welcome as the plugin is working almost as i'd like it.

Thanks
Matt

  • Milan

    Hello Matt,

    I hope you are well today and thanks for asking us. :slight_smile:

    What I understand from your question is that you want to access currently processing user's data while syncing operation goes on. ? Right ? If yes then I've contacted developer to get his valuable view on this. As he is not currently online, I will post back his view on this one as soon as hear something from him.

    If I have headed on completely wrong path please let me know with some more information. :slight_smile:

    Cheers,
    Milan

  • Matt Boden

    Hi Milan,

    Hopefully i've explained clearly (probably not).

    In essence i want to be able to attribute activity from site B (by a user) and show that info on Site A (by that same user). This would mean, i think, that a current user is the same on both sites. I'm not sure this is the case? Certainly not in terms of they're ID as that differs for a user between the 2 sites once synced. Each site has it's own user table (even though the database is shared). Maybe i'm approaching this in the wrong way, and maybe the user tables need to be the same for both sites (so shared user tables). If this is the case, i'm not sure if the sync plugin will be defunct.

    Welcome your thoughts.

    Hopefully i've not confused you further....

  • Tristan Fagan

    I had a similar issue in that I was exporting custom post types to a second site, but the author ID was different from the current user.

    I got around this by adding a custom user meta field called master_id and storing the original user ID within that field. On the slave site, I queried each of the custom post types based on the current users master_id value rather than their actual user id.

    Hope that helps someone :slight_smile:

  • Adam Czajczyk

    Hello Matt!

    I think there may be another way :slight_smile:

    The User Sync plugin keeps users of both sites "in sync" and you are right about the ID. That doesn't matter whether the database of both sites is the same or not: as long as these are separate instances of WordPress, the user table is separate and since the ID is added automatically, it will differ.

    However, since these are the same users, their e-mail addresses should be the same on both sites and it's a unique identifier because WordPress doesn't allow two user accounts using the same e-mail.

    Let's say that you would like to sync content for a user from master to sub-site. Here's how I imagine this (pseudo code below):

    # on sub-site
    $user_id = get_current_user_id(); // get an ID of a current user
    // get user e-mail
    $user_info = get_userdata($user_id);
    $user_mail = $user_info->user_email;
    
    // get content from master site
    $user_content = get_content_from_master_site($user_mail);
    
    // update content on sub-site
    update_user_content_on_subsite($user_id);
    
    # this part should work on master site
    
    $search_user = get_user_by('email', $user_mail);
    $user_id = $search_user->ID;
    
    return_user_content($user_id);

    That's a pseudo-code (except the very beginning where we get user ID and the part where we get user e-mail and convert it to back to user ID) but should give you a foundation for further development. The basic idea is to first grab user e-mail, then connect to the other site sending that e-mail to it, find an ID of a user with that e-mail there and return assigned content in response.

    I hope that helps!

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.