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!

Related posts:

  1. How To: Redirect A User After Logging Into WordPress
  2. How To: Create Backdoor Admin Access in WordPress
  3. How To: Hide an Admin Menu in WordPress
  4. How To: Add a Post Thumbnail to an RSS Feed in WordPress
  5. How To: Save Taxonomy Meta Data as an Options Array in WordPress
Enjoy this post? Be sure to subscribe to my RSS feed and my WordPress Tips and Tricks Newsletter! Also check out my new book: Professional WordPress

About Brad
I am the CEO and Co-Founder of WebDevStudios.com, a co-host on the SitePoint Podcast, and the co-author of Professional WordPress.

Comments

  1. Mo Jangda says:

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

  2. Brad says:

    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. Mo Jangda says:

    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. Andy Bailey says:

    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.

Speak Your Mind

*