I am currently building a network of blogs for individual real estate agents. Currently, a broker will have a main site, with agents sites added as subdirectories, e.g., www.example.com and www.example.com/agent. Each agent's front-page.php template has an area which displays the agents contact info. I am currently using get_user_meta() in my theme template to access user information from the user profile, but I have quickly realized that when I access that data, there is no relative way to access the user of a specific subdiretory's information. When I use this code:

echo get_user_meta(2, 'display_name', true);

in the theme template, it defaults on every install of that theme across the network to the user_id=2s information. Logically I understand why. But how can I get Joe's info to show up on www.example.com/joe and Chris's info to show up on www.example.com/chris? I am looking for a solution that can be defined within the admin somehow, so scaling this theme is as simple as possible.

EDIT: I guess now that I think about it, a possible solution would be to create a theme options page using get_option to call entered information. I was just hoping there was an easier way retrieving user information since some of it is already created during user creation. If you think you have a good solution to this problem, I think it would be worth writing about. I have not been able to find any info regarding user_meta and multisite in this way.


2 Answers 2


The problem is that the idea of "the user of a specific subdirectory" doesn't really exist natively in WP. There are sites ("subdirectories"), and there are users, and users can be associated with sites (through Roles and Capabilities), but there is no guarantee of a one-to-one correspondence.

If you are running a network where users have registered for their own blogs, then there's a good likelihood that you could use the 'primary_blog' usermeta item to get the information you need. Put the following function in a network activated plugin:

function wpse_50705_get_primary_user_for_blog() {
    global $wpdb;
    return $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'primary_blog' AND meta_value = %d", get_current_blog_id() ) );

And then, in your template file,

echo get_user_meta( wpse_50705_get_primary_user_for_blog(), 'display_name', true );

This should work in most cases where users each have a single primary blog - no one has more than one, and there's no blog that more than one user has as a primary blog.

  • this worked, but I decided a simple theme option was the more logical way to go. thanks boone, maybe it'll clear it up for someone
    – chris_s
    May 3, 2012 at 2:52

If the subdirectory name will always match the username of the person you want to display, you can do this:

global $current_blog;
$username = trim($current_blog->path, '/');
if ($user_id = user_exists($path))
    echo get_user_meta($user_id, 'display_name', true);
    echo "User not found: $username";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.