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!