How To: Load User Info Using the Admin Email in WordPress

Today’s handy WordPress code snippet is a simple way to retrieve user data based on the administrator email in WordPress. The email account I am referring to is the one listed under Settings > General and is the main admin email for your website.

$admin_email = get_option('admin_email');
$admin_user_id = get_user_id_from_string($admin_email);
$user_info = get_userdata($admin_user_id);

The above code example first loads the admin email from the WordPress options. Next it determines that user’s ID based off of their email address using the get_user_id_from_string() function. Finally we use get_userdata() to load all user data for that user ID.

Currently the get_user_id_from_string() function is only available in WordPress MU. I have confirmed however that this function does exist in WordPress 3.0. That means after the merge this function will be available to all sites running WordPress.

We can also use the get_user_by_email() function included since WordPress 2.5 to accomplish the same task. Thanks to Mo Jangda for pointing that out in the comments. Below is an example using this method:

$admin_email = get_option('admin_email');
$user_info = get_user_by_email($admin_email);

This is actually a more efficient method as we don’t need to call the function to retrieve the user ID first. In the world of WordPress you learn something everyday. Thanks Mo Jangda!

Enjoy this post? Be sure to subscribe to my RSS feed and my WordPress Tips and Tricks Newsletter! Also check out my WordPress books: Professional WordPress Third Edition & Professional WordPress Plugin Development

Comments

  1. You could probably just use get_user_by_email() instead and skip the whole getting the id bit. Should work 2.5+.

  2. Very cool! That’s why I love posting these snippets because you learn about other ways to accomplish the same task. There are so many functions in WordPress it’s tough to track them all. I’ll test it out and add your example to my post.

  3. Agreed! Too many functions, too little time 🙂

    If you want something to completely blow your mind, check out get_user_by() (http://core.trac.wordpress.org/browser/trunk/wp-includes/pluggable.php#L184). It lets you load a user based on one of 4 fields: ID, nicename, email, login.

  4. you can get the email with get_blog_option if you have the blog_id

    I have only blog_id so I had to use get_blog_option($blog_id,’admin_email’);

    then I searched for get user by email and ended up here. nice!

    tweeted.

  5. where do i find the information from my admin access data for my website to start creating blogs for the site?