I am working on creating a function that will take a $blog_id
and return the corresponding $user_id
of the user who owns/created the blog in a multi site network.
public function SelectOwnerUserIdFromBlogId( $blog_id ) { ... }
I have been reviewing the database and the closest thing I can find is looking at the wp_usermeta table and filtering on capabilities but this feels too hackish. Any better idea?
SELECT user_id as UserId,
IF(SUBSTRING_INDEX(SUBSTRING_INDEX(meta_key, '_', 2), '_', -1) = 'capabilities', 1, SUBSTRING_INDEX(SUBSTRING_INDEX(meta_key, '_', 2), '_', -1)) as BlogId
FROM wp_usermeta
WHERE meta_key like '%_capabilities'
AND meta_value like '%administrator%'
I think I figured out the query. Just replace the blog_id in the WHERE condition to get the desired user ID.
SELECT meta_value as UserId
FROM wp_blogs b
JOIN wp_sitemeta sm
ON sm.site_id = b.site_id and sm.meta_key = 'admin_user_id'
WHERE b.blog_id = 2
This is how i am doing it:
Hook into the wpmu_new_blog action which will be fired when a new site is created.
In your function for this, do the following:
add_site_option( 'site_author', $user_id );
Site authors will now be added to the site options.
Can't speak to anything earlier, but as of the current version (4.9.7), you can use network options to achieve this.
add_action('wpmu_new_blog', 'record_site_operator', 10, 2);
function record_site_operator($blog_id, $user_id) {
update_network_option(NULL, 'site_operator_'.$site_id, $user);
}