Support » Developing with WordPress » get site id for user

  • I’m having some problems finding the correct php to get the site id number (multisite) that a logged in user is assigned to so I can dynamically change a link to take them to the correct content regardless of which site they are on.

    So, if you are a user on the EMEA site, the Sites ID in WP network admin would be 6. If you are a user on the original site, the Sites ID in WP network admin would be 2. Here is where I am at with the section of the code that determines what the slug portion of the link should be:

    if (is_user_logged_in()) {
    	$the_user = wp_get_current_user();
            $the_user_site_id = $the_user->site_id;
    	echo "<!-- DEBUG - the_user_site_id: $the_user_site_id -->";
    	if ( $the_user_site_id == "2" ) {
    		$l_link = '/account/';
    		echo "<!-- DEBUG - l_link: $l_link -->";
    	}
    	else if ( $the_user_site_id == "6" ) {
    		$l_link = '/emea/account/';
    		echo "<!-- DEBUG - 32 - l_link: $l_link -->";
    	}
    }
    $the_user = wp_get_current_user(); 
    $the_user_site_id = $the_user->site_id;

    is where the magic happens but I can’t figure out which function I should use and I’ve tried many.

    • This topic was modified 1 year, 6 months ago by  Steve Stern.
    • This topic was modified 1 year, 6 months ago by  StephenEquus.
    • This topic was modified 1 year, 6 months ago by  StephenEquus.
    • This topic was modified 1 year, 6 months ago by  StephenEquus.
    • This topic was modified 1 year, 6 months ago by  StephenEquus.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Trying:

    $the_user = get_blogs_of_user();
    $the_user_site_id = $the_user->userblog_id;
    echo “<!– DEBUG – 26 – the_user_site_id: $the_user_site_id –>”;

    But it doesn’t return a number – returns nothing.

    Moderator bcworkz

    (@bcworkz)

    You should var_dump($the_user); so you can see what you are working with. The returned array is keyed by site ID. So you need to know the site ID to get the site ID!

    The problem is in theory a user can belong to any number of sites. There’s nothing in the returned array from get_blogs_of_user() which would indicate in which site they can administer. In fact, they could possibly administer several sites. If on your site, users only belong to the base site and their own, and never anyone else’s, the returned array will always have 2 items. One key is 2 and the other is the site ID to which they are assigned.

    If your situation is not that straight forward, you can at least determine for which sites a user is administrator by querying user_meta table by user ID, meta_key like ‘%capabilities’, meta_value like ‘%administrator%’. Determine the site ID of each record returned by the meta_key value. Typical keys would be wp_capabilities, wp_2_capabilities, wp_6_capabilities, etc.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get site id for user’ is closed to new replies.