Last active
February 5, 2019 09:57
-
-
Save Joel-James/b7e1c1c0a951737d613364ea0032a972 to your computer and use it in GitHub Desktop.
Get registered sites of a user in WordPress multisite.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get user's registered blogs/sites. | |
* | |
* @param int|bool $user_id User id or current user. | |
* | |
* @return array | |
*/ | |
public static function get_registered_sites_of_user( $user_id = false ) { | |
global $wpdb; | |
$sites = array(); | |
// Make sure user id is ready. | |
$user_id = empty( $user_id ) ? get_current_user_id() : $user_id; | |
// Get user details. | |
$user = get_userdata( $user_id ); | |
// We need user name. | |
if ( empty( $user->user_login ) ) { | |
return $sites; | |
} | |
// Get the signups of the user. | |
$signups = $wpdb->get_results( | |
$wpdb->prepare( | |
'SELECT domain, path FROM ' . $wpdb->base_prefix . 'signups WHERE user_login = %s', | |
$user->user_login | |
) | |
); | |
// If sites are empty, return. | |
if ( empty( $signups ) ) { | |
return $sites; | |
} | |
// Loop through each site. | |
foreach ( $signups as $signup ) { | |
if ( isset( $signup->domain, $signup->path ) ) { | |
$blog_id = get_blog_id_from_url( $signup->domain, $signup->path ); | |
if ( ! empty( $blog_id ) ) { | |
$sites[] = $blog_id; | |
} | |
} | |
} | |
/** | |
* Filter for sites of a user. | |
* | |
* @param array $sites Sites of user. | |
*/ | |
return apply_filters( 'get_registered_sites_of_user', $sites ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment