0

I'm having a problem with echoing the admin's profile meta in a custom post type template. It used to work, but for some reason it's not working anymore. I'm not aware of any changes made that would cause such behavior.

This is the code I'm using in the loop:

<p>
<?php
$thisblog = $current_blog->blog_id;
$user_id_from_email = get_user_id_from_string( get_blog_option($thisblog, 'admin_email'));
$phone_number = get_user_meta($user_id_from_email, 'phone', true);
if($phone_number!='') { ?>
<?php echo $phone_number; ?>
<?php } else { ?>
<?php } ?></p>

I've also tried this:

<?php
global $current_blog;
$thisblog = $current_blog->blog_id;
$user_id_from_email = get_user_id_from_string( get_blog_option($thisblog, 'admin_email'));
$details = get_userdata($user_id_from_email);
if ($details->nickname) echo $details->nickname;
?>

And this:

<?php
global $current_blog;
$thisblog = $current_blog->blog_id;
$user_id_from_email = get_user_id_from_string( get_blog_option($thisblog, 'admin_email'));
$firm_name = get_user_meta($user_id_from_email, 'nickname', true);
if($firm_name!='') { ?>
<?php echo $firm_name; ?>
<?php } else { ?>
<?php } ?>

That echoes nothing. The following code echoes some data, but it seems to be doing so for the super admin:

<?php $user_info = get_userdata(1);
      echo $user_info->last_name .  ", " . $user_info->first_name . "\n";
?>

Any ideas as to what's wrong?

6
  • 1
    I dont see where $current_blog is defined. You need to add global $current_blog before using it. For more information you can check the codex Jan 22, 2014 at 6:45
  • I forgot to mention i did that as well, ill update my question. @MarutiMohanty
    – 730wavy
    Jan 22, 2014 at 17:22
  • As an aside, you can use get_current_blog_id() instead of globalizing $current_blog.
    – Pat J
    Jan 22, 2014 at 17:37
  • Im not sure what you mean. I wont be able to provide the blog id cause it has to work for all sites made on the network. @PatJ
    – 730wavy
    Jan 22, 2014 at 18:02
  • I mean, there's a WordPress function -- get_current_blog_id() -- that will return the current blog ID, thus removing the need for your global $current_blog; $thisblog = $current_blog->blog_id; code. You can use $thisblog = get_current_blog_id(); instead.
    – Pat J
    Jan 22, 2014 at 19:11

1 Answer 1

0

According to the Codex, get_user_id_from_string() is deprecated, and should be replaced with get_user_by().

<p>
<?php
$thisblog = get_current_blog_id();  // probably don't need this anymore
$admin_email = trim( get_option( 'admin_email' ) );
$admin_user = get_user_by( 'email', $admin_email ) );
$phone_number = get_user_meta($admin_user->ID, 'phone', true);
if($phone_number!='') { ?>
<?php echo $phone_number; ?>
<?php } else { ?>
<?php } ?></p>

I've also replaced your get_blog_option() with the simpler get_option(), since get_blog_option() essentially does switch_to_blog(), get_option(), and restore_current_blog(). Since you're going after the information in the current blog anyway, the switch_to_blog() and restore_current_blog() are superfluous.

If this code doesn't work, do some debugging -- add in var_dump( $admin_email) to verify that the get_option() retrieved something, etc.

Edited to respond to comments

When you create a new site in WordPress Multisite, you are asked for an administrative email address. That user will be assigned as the site's first administrator. However, if that user is then removed from the site, the admin_email option is not removed from the database.

Check your user list in your wp-admin pages, and make sure that there is, in fact, an administrator-level user with exactly the same email address that you got from your var_dump( $admin_email ); call. If there is, count the number of characters in the email address. I'm wondering if you're seeing a whitespace issue (eg, an extra space inserted in the $admin_email or something). I've added trim() to the code sample, but I may be grasping at straws.

7
  • Ok i tried it, and it didnt echo nothing out, but when i added var dump. it did echo out the admin email.
    – 730wavy
    Jan 23, 2014 at 20:41
  • All right. What about var_dump( $admin_user ); & var_dump( $phone_number ); after their respective definitions?
    – Pat J
    Jan 23, 2014 at 21:10
  • I got this result - bool(false) bool(false)
    – 730wavy
    Jan 23, 2014 at 23:11
  • with this code <?php $admin_email = get_option( 'admin_email' ); $admin_user = get_user_by( 'email', $admin_email ); $phone_number = get_user_meta($admin_user->ID, 'phone', true); var_dump( $admin_email); var_dump( $admin_user ); var_dump( $phone_number ); if($phone_number!='') { ?> <?php echo $phone_number; ?> <?php } else { ?> <?php } ?>
    – 730wavy
    Jan 23, 2014 at 23:12
  • Updated my answer with some more suggestions.
    – Pat J
    Jan 23, 2014 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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