***Important, this is related to WordPress Multisite and will be used for "sites inside the network"****
write me a simple script that I can re-use in my template that will help me do the following:
1) Find the site owner - this is the person that actually registered the site
2) Get value from user meta table - I should be able to just change the slug to get different meta data field
Thanks
Navjot Singh answers:
You can use [[LINK href="http://wordpress.stackexchange.com/a/36169/5730"]]this solution[[/LINK]] to find the any blog's administrator in a multisite setup.
And try this sample code to get the first name of the current blog's user.
$user_id_from_email = get_user_id_from_string( get_blog_option(get_current_blog_id(), 'admin_email'));
$current_site_admin = get_userdata($user_id_from_email);
$fname = (get_user_meta($current_site_admin, 'first_name', true));
wpcoach comments:
Thanks for the suggestion,
I checked the link and used this:
$blog_id = get_current_blog_id();
$querystring = "SELECT `user_id`
FROM `wp_usermeta`
WHERE (meta_key LIKE 'primary_blog' AND meta_value LIKE $blog_id)
LIMIT 1";
$blogownerid = $wpdb->get_var($querystring);
echo $blogownerid;
The correct id was returned, now how can this be modified so I can get meta from this user - please adjust so I can reuse in more than one spot in the template.
Thanks
Navjot Singh comments:
Here it is modified.
$blog_id = get_current_blog_id();
$querystring = "SELECT `user_id`
FROM `wp_usermeta`
WHERE (meta_key LIKE 'primary_blog' AND meta_value LIKE $blog_id)
LIMIT 1";
$blogownerid = $wpdb->get_var($querystring);
echo $blogownerid;
$user_id_from_email = get_user_id_from_string( get_blog_option($blogownerid, 'admin_email'));
$current_site_admin = get_userdata($user_id_from_email);
$fname = (get_user_meta($current_site_admin, 'first_name', true));
Now $blogownerid is the id and $fname is the first name.
wpcoach comments:
I don't understand, I am only getting echo of the $blogownerid, when I echo $fname I don't get any results
Navjot Singh comments:
Ok. Try this
$blog_id = get_current_blog_id();
$querystring = "SELECT `user_id`
FROM `wp_usermeta`
WHERE (meta_key LIKE 'primary_blog' AND meta_value LIKE $blog_id)
LIMIT 1";
$blogownerid = $wpdb->get_var($querystring);
echo $blogownerid;
$fname = (get_user_meta($blogownerid, 'first_name', true));
wpcoach comments:
Worked perfectly - thanks.
Navjot Singh comments:
Your welcome.
Naveen Chand answers:
<?php
$sitedomain ="example.com";
$path="/";
$myarray = get_admin_users_for_domain($sitedomain, $path);
foreach($myarray as $x)
{
$this_site_admin = $x['ID'];
echo "This is the User ID: " .$this_site_admin;
}
$user_info = get_userdata($this_site_admin);
$username = $user_info->user_login;
$first_name = $user_info-> user_firstname;
$last_name = $user_info-> user_lastname;
?>
wpcoach comments:
but how do I set: $sitedomain ="example.com"; - this is multisite - when a person registers they are given a subdomain also, I can't check by domain name anyway because I allow domain mapping to sites.
There has to be some way to identify the site dynamically.
any thoughts
wpcoach comments:
This is how I updated to get site domain:
$sitedomain = bloginfo('home');
$path="/";
$myarray = get_admin_users_for_domain($sitedomain, $path);
foreach($myarray as $x)
{
$this_site_admin = $x['ID'];
echo "This is the User ID: " .$this_site_admin;
}
$user_info = get_userdata($this_site_admin);
$username = $user_info->user_login;
$first_name = $user_info-> user_firstname;
$last_name = $user_info-> user_lastname;
The results of the above only displays the site url - but it is the correct url.
Naveen Chand comments:
You can try this. I have not tested.
<?php
$mysites = $wpdb->get_results(
"
SELECT *
FROM $wpdb->blogs
"
);
foreach ( $mysites as $mysite )
{
$mysitename = "'" .$mysite->domain ."'";
$mypath = "'" .$mysite->path ."'";
echo "This is the Sitename: " .$mysitename;
echo " This is the path: " .$mypath;
$myarray = get_admin_users_for_domain( $mysitename, $mypath );
foreach ($myarray as $x)
{
$this_site_admin = $x['user_login'];
echo "This is the User ID: " .$this_site_admin;
}
}
?>
wpcoach comments:
output is below the blog title:
http://apfree.approvedprofessional.com/
This is the Sitename: 'approvedprofessional.com' This is the path: '/'This is the Sitename: 'apfree.approvedprofessional.com' This is the path: '/'This is the Sitename: 'aaron247.approvedprofessional.com' This is the path: '/'This is the Sitename: 'testperson2.approvedprofessional.com' This is the path: '/'This is the Sitename: 'testhome.approvedprofessional.com' This is the path: '/'This is the Sitename: 'whmcstest.approvedprofessional.com' This is the path: '/'This is the Sitename: 'anothertest.approvedprofessional.com' This is the path: '/'
seems that it is just looping through all sites in the network showing site name and path
Naveen Chand comments:
Okay. Did it not echo the user ID?
echo "This is the User ID: " .$this_site_admin;
Naveen Chand comments:
Well, lets try to remove the quotes around sitename and path. Here is the updated code:
<?php
$mysites = $wpdb->get_results(
"
SELECT *
FROM $wpdb->blogs
"
);
foreach ( $mysites as $mysite )
{
$mysitename = $mysite->domain;
$mypath = $mysite->path;
echo "This is the Sitename: " .$mysitename ."</br>";
echo " This is the path: " .$mypath ."</br>";
$myarray = get_admin_users_for_domain( $mysitename, $mypath );
foreach ($myarray as $x)
{
$this_site_admin = $x['user_login'];
echo "This is the User ID: " .$this_site_admin ."</br>";
}
}
?>
Atleast in this attempt, I am hoping that the user ids of each of the site creator will be displayed. To explain what I am trying to do is.. in the first part of the code, I am querying the blogs table and pick only site domain and path for each of your sites. Once we get that, for each site domain and path, we then use the <strong>get_admin_users_for_domain</strong> function to fetch the user id of the admin of that site. Once we get that, we can then get any meta data of tht user id.
pls try the above code and let me know.
wpcoach comments:
it is looping through all sites in the network:
This is the Sitename: approvedprofessional.com
This is the path: /
This is the Sitename: apfree.approvedprofessional.com
This is the path: /
This is the Sitename: aaron247.approvedprofessional.com
This is the path: /
This is the Sitename: testperson2.approvedprofessional.com
This is the path: /
This is the Sitename: testhome.approvedprofessional.com
This is the path: /
This is the Sitename: whmcstest.approvedprofessional.com
This is the path: /
This is the Sitename: anothertest.approvedprofessional.com
This is the path: /
No information is showing