Getting $user_id for the_author_meta outside the loop in multisite

Here's a doosey for ya. So I am currently running a multisite install where the super admin creates all new sites. One of the templates we're using is a profile page for a loan officer. I am extending the user profile so the new user can input critical data that needs to be displayed on their page. What I have come to find, is that you need to specify the user ID if you want to use the function the_author_meta(); outside of the loop. Well here's my problem. I can't put an ID in the template, because each new loan officer is going to be getting an install of this theme. So how can I make this feature dynamic? Do I need to query a field in the DB? Does each new site get it's own table for me to access this information? I'm not a wizard at querying DB tables dynamically just yet, so I'm hoping someone has a solution. Below is the profile section for the LO to help visually if need be.

http://www.diigo.com/item/image/27x2l/o1kp

  • Timothy Bowers

    Hey Chris.

    Have you tried the get current user function:

    http://codex.wordpress.org/Function_Reference/wp_get_current_user

    <?php
        wp_get_current_user();
        /**
         * @example Safe usage: $current_user = wp_get_current_user();
         * if ( !($current_user instanceof WP_User) )
         *     return;
         */
        echo 'Username: ' . $current_user->user_login . '<br />';
        echo 'User email: ' . $current_user->user_email . '<br />';
        echo 'User first name: ' . $current_user->user_firstname . '<br />';
        echo 'User last name: ' . $current_user->user_lastname . '<br />';
        echo 'User display name: ' . $current_user->display_name . '<br />';
        echo 'User ID: ' . $current_user->ID . '<br />';
    ?>

    Thats an example from their Codex. You could get the UID there :slight_smile:

    Take care.

  • Timothy Bowers

    Sorry we are talking of a profile type page here right, like "click here for you profile" and then have all of the current users information?

    If so then wp_get_current_user will get the current user, IE whom ever is signed on, so if it were me then it would get my ID and if it were you then it would be your ID.

    Perhaps I misunderstood your end goals.

    If its more like an author page, ie profile about someone else then perhaps start here:

    http://codex.wordpress.org/Author_Templates

    Take care.

  • chris_s

    Hey Tim,

    So I tried looking into the author template but I'm not sure that's what I need. If you take a look at the image I put in my first post, you'll see I'm just accessing simple profile information. But what I need to do, is be able to access a specific user. Typically, the Super Admin will be a user on every site, correct? So in addition to the Super User, I will be creating one other user that will be the unofficial "owner" of the site. Example: http://www.example.com/chris. When this site is created, it will use the same Loan Officer Template that all other users get. But "Chris's" site will need to read his $first_name, $description, etc. What I need to do is make this section of the template dynamic to the user I create for each site. I really don't know if I'm missing something stupid, but I just can't logically figure this out. I think the issue is that it's outside of the loop, so how does WordPress get this info if it's not predefined?

    Is my other option just a get_option() from the profile page and echo it in the template? I feel like I should just be able to hook into a user's profile...

    Have you used WP_Users_Query? I'm not sure how it works yet, but was suggested elsewhere.

    Thanks

  • Timothy Bowers

    Hey again.

    Typically, the Super Admin will be a user on every site, correct? So in addition to the Super User, I will be creating one other user that will be the unofficial "owner" of the site. Example: http://www.example.com/chris. When this site is created, it will use the same Loan Officer Template that all other users get.

    You would need some way to define that then. Either an options page in the backend. or by looping through the site level admins.

    So sure you could use:

    http://codex.wordpress.org/Class_Reference/WP_User_Query

    To get the admins, that could work.

    And

    http://codex.wordpress.org/Class_Reference/WP_User

    Hope that helps.

  • chris_s

    The only admins on these sites will be the Super Admin. The Loan Officers will be Authors since they only permissions they should have are to post and edit their posts. I don't know if this will work, but do you think I'm close?

    function loan_officers() {
    	global $wpdb;
    
    	$authors = $wpdb->get_results("
    		SELECT  ID, display_name
    		FROM wp_users
    		INNER JOIN wp_usermeta
    		ON wp_users.ID  = wp_usermeta.user_id
    			WHERE wp_usermeta.meta_key = 'wp_capabilities'
    		AND wp_usermeta.meta_value
                            LIKE '%author%'
    		ORDER BY display_name
    		");
    
    		foreach($authors as $author) {
    			echo "<li>";
    				echo '<div class="contribute-avatar"';
    				echo "<a href=\"" . home_url() ."/author/"; the_author_meta('user_nicename', $author->ID); echo "/\">";
    				echo get_avatar($author->ID, '75');
    				echo "</a>";
    				echo "</div>";
    				echo '<div class="contribute-text">';
    				echo "<h3><a href=\"". home_url() ."/author/"; the_author_meta('user_nicename', $author->ID); echo "/\">"; the_author_meta('display_name', $author->ID);
    				echo "</a></h3>";
    				echo "<p>";
    				echo the_author_meta('description', $author->ID);
    				echo "</p>";
    				echo "<p>";
    				echo _e('Browse Articles by ','periodic');
    				echo "<a class=\"right-arrow\" href=\"". home_url() ."/author/"; the_author_meta('user_nicename', $author->ID); echo "/\">"; the_author_meta('display_name', $author->ID);
    				echo "</a>";
    				echo "</p>";
    				echo "</div>";
    			echo "</li>";
    		}
    	}
  • Timothy Bowers

    Hey again.

    Editors or admins, the principle is the same.

    Stop swapping between " and ' it will cause you head aches later. Stick to one or the other. Then escape the other when needed. I would use " in HTML and ' for the rest. Your choice.

    Your div table on line 18 is not closed. and there are other issues there, echo's within echos?

    For example:

    echo "<a href=\"" . home_url() ."/author/"; the_author_meta('user_nicename', $author->ID); echo "/\">";
    	echo get_avatar($author->ID, '75');
    	echo "</a>";

    Could be:

    echo '<a href="' . home_url() . '/author/'. the_author_meta("user_nicename", $author->ID) .'">';
    	echo get_avatar($author->ID, '75');
    	echo '</a>';

    You can see the difference. If you tested the whole thing and then deal with errors as you fix others. I'm guessing you are relatively new to php? (Sorry if I missed that somewhere).

    Also home_url() brings the home url, including the http:// so you using / first would get you /http://

    Once all the syntax and everything is done then you can test out different methods until you find the right way. Unfortunately though this is out of the scope of our support.

    Hope that helps. :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.