• Resolved virtualgeorge

    (@virtualgeorge)


    I’m not a coder so please be patient LOL.

    So I setup MU and have a form where users can create sites with a few default pages and theme in their new site.

    I wanted to out put some of their info on the contact or about us pages so trying to figure that out. So from the help docs I created the below code which outputs the name & email & website of the current logged in user.
    However, that was a bad idea as non-logged users see nothing.
    So what could I use so that it gets the current info from the site or the site admin instead of the current user so the info can be displayed to everyone? Or what would be the best way do that? THX!!

    <?php
        $current_user = wp_get_current_user();
        /**
         * @example Safe usage: $current_user = wp_get_current_user();
         * if ( !($current_user instanceof WP_User) )
         *     return;
         */
    
        echo '<span class="cpage">Name:&nbsp;&nbsp;<span style="margin-right:8px;"> ' . $current_user->user_firstname . '</span>' . $current_user->user_lastname . '</span><br />';
        echo '<span class="cpage">Email:&nbsp;&nbsp;   ' . $current_user->user_email . '</span><br />';
    ?>
     <?php
    $user_id = 1;
    $user_blogs = get_blogs_of_user( $user_id );
    foreach ($user_blogs AS $user_blog) {
    }
    echo '<span class="cpage">Web Site:&nbsp; '.$user_blog->siteurl.'</span><br>';
    
    ?> 
    
    </div>
Viewing 13 replies - 1 through 13 (of 13 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Hai George,

    Saw this same question on the jobs page of WPMU DEV.
    And because I couldn’t get into touch with you on that site, I thought to reply here.

    When you want to show information in a header template, I would suggest to use a function instead of a short code.
    The function you are looking for could be like this, to be included in the file functions.php:

    function z_show_current_name(){
    	// Get the current admin user, using it's registered email as key
    	$user_info = get_user_by( 'email', get_option('admin_email'));
    	// Use the user_info found to distill the info wanted
    	$user_fname = $user_info->first_name;
    	$user_lname = $user_info->last_name;
    	$text = 'TESTING: ';
    	// Return the output to show
    	return '<span class="lmn_user_info lmn_user_fname">' . $text . '' . $user_fname . '</span><span class="lmn_user_lname"> ' . $user_lname . '</span>';
    }

    You could then use this function in your header.php file like this:

    <?php echo z_show_current_name(); ?>

    Hope this will help.
    If you want to support me, you can buy me a coffe via my site at de-baat dot nl slash contact.

    Thread Starter virtualgeorge

    (@virtualgeorge)

    Hey DeBAAT, that worked, thanks so much!!!
    Bought you some coffee:)

    This post is a month old but job you just saw on wpmudevs job board where I needed the output in the header was also Posted on 4 different forums & wpmudev’s support forum but had not got a working solution yet.

    Many Thanks!
    -George

    Hai George,
    Glad to read it is working.
    And saw your coffe tip coming in.
    Thanks for that!

    Thread Starter virtualgeorge

    (@virtualgeorge)

    If I wanted to do something similar to the above to show the phone number from the users profile, how could that be done? I already have a shortcode that works on pages to display the phone number:

    add_shortcode( 'user_phone' , 'z_get_current_phone' );
    function z_get_current_phone( $atts ){
    	$user_phone = get_the_author_meta('phone');
    	extract( shortcode_atts( array(
            'text' => null,
        ), $atts ) );
        return '<span class="lmn_user_info lmn_user_phone">' . $text . '' . $user_phone . '</span>';
    }

    So what could I add to functions so that I could add this to the header.php?
    <?php echo z_show_current_phone(); ?>

    Tried the below in my functions but didn’t work, is it close? ??
    thanks!

    function z_show_current_phone(){
    	$user_phone = get_the_author_meta('phone');
    	extract( shortcode_atts( array(
            'text' => null,
        ), $atts ) );
        return '<span class="lmn_user_info lmn_user_phone">' . $text . '' . $user_phone . '</span>';
    }
    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    get_the_author_meta – Are you pulling from the author into an info box or something on the article page? If it’s outside the loop that may be why it’s not working. Try pulling ANY author data, see what you get.

    Thread Starter virtualgeorge

    (@virtualgeorge)

    I paid someone to create a plugin for multisite so when a person registers and creates a site they enter their personal info name, email, phone etc and that is displayed on the profile page and I can use shortcodes to display that on pages & posts. But I guess it is outside of the loop as I was told by others when I first this. So I am really not sure what your asking? I just thought I would ask in case it was something easy before I went back to paying someone to code this for me:)

    thanks!

    Hai George,

    You’re close, but not complete as you already thought.
    You can use the same tactic as I’ve shown you before.
    The trick here is that the get_user_meta() function needs a user ID to get the correct information.

    Try the following:

    function z_show_current_phone(){
    	// Get the current admin user, using it's registered email as key
    	$user_info = get_user_by( 'email', get_option('admin_email'));
    	// Use the user_info found to distill the info wanted
    	$user_phone = get_user_meta( $user_info->ID, 'phone' );
    	$text = 'TESTING: ';
    	// Return the output to show
    	return '<span class="lmn_user_info lmn_user_phone">' . $text . '' . $user_phone . '</span>';
    }

    Then you can use this function the same way as for showing the name.

    Thread Starter virtualgeorge

    (@virtualgeorge)

    Thanks DeBAAT, I added that function and it displays the word “array” instead of the phone number?

    As you can see at the top of this page in the header following the avatar & name that you helped me with before:

    https://evantage360.com/default/

    Any more advice when you have time? ??

    thanks again !

    Try changing:
    $user_phone = get_user_meta( $user_info->ID, 'phone' );

    to:
    $user_phone = get_user_meta( $user_info->ID, 'phone', true );

    Thread Starter virtualgeorge

    (@virtualgeorge)

    Thanks csloisel, I did add that & diplays nothing:

    TESTING:

    without adding ‘, true’ it shows the word array:
    TESTING: Array

    Are you sure that meta field exists and has a value?

    Hai,
    Could indeed be that the user you’re querying has no phone defined.
    You could try the following:

    function z_show_current_phone(){
    	// Get the current admin user, using it's registered email as key
    	$user_info = get_user_by( 'email', get_option('admin_email'));
    	// Use the user_info found to distill the info wanted
    	$user_meta = get_user_meta( $user_info->ID );
    	if (isset($user_meta['phone']) ) {
    		$user_phone = $user_meta['phone'][0];
    	} else {
    		$user_phone = 'No phone found';
    	}
    	$text = 'TESTING: ';
    	// Return the output to show
    	return '<span class="lmn_user_info lmn_user_phone">' . $text . '' . $user_phone . '</span>';
    }

    The main difference is that I now get all meta info into $user_meta.
    And then check for the existence of the ‘phone’ value.
    For more info on get_user_meta, you could check the Codex:
    https://codex.www.ads-software.com/Function_Reference/get_user_meta

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘How to output name & email of site admin’ is closed to new replies.