skip to Main Content

I need to display the username of the main site administrator for a part of my template.

I have used the following short code to display the email address of the main administrator of the site with which he started WordPress:

<?php bloginfo('admin_email'); ?>

But I did not find any short code to display the username of the main administrator of the site.

Is there such a shortcode or should I get the username of the main administrator of the site in another way?

2

Answers


  1. There is not a shortcode that I know of, but writing one is very simple, and a great exercise to learn shortcode creation:

    function admin_email_shortcode() { 
     return bloginfo('admin_email');
    }
    
    add_shortcode('admin_email', 'admin_email_shortcode');
    

    Simply add that code to your themes functions.php file and use the shortcode [admin_email] where you want the email address to be displayed.

    As a bonus, here’s another shortcode for you that would generate a list of all site Admins if there was more than one:

    function admin_emails_shortcode() { 
     $admins = get_users('role=Administrator');
     foreach ($admins as $admin) {
      $adminList .= $admin->user_email.", ";
     }  
      return rtrim($adminList, ', ');
     }
    
    add_shortcode('admin_emails', 'admin_emails_shortcode');
    

    To get the FIRST administrators email address:

    function first_administrator_email() { 
     $adminsArray = array();
     $admins = get_users('role=Adviser'); //Administrator
     foreach ($admins as $admin) {
      $adminsArray[$admin->id] = $admin->user_email;
     }  
     
     return $adminsArray[min(array_keys($adminsArray))];
    }
    
    add_shortcode('first_admin_email', 'first_administrator_email');
    
    Login or Signup to reply.
  2. if you want to get the user for the email that the admin set during setup you can do so by combining bloginfo('admin_email') and get_user_by() functions.

    Something similar to:

    $username = 'Unknown'; // Default name in case WP_User could not be found.
    $admin_email = bloginfo('admin_email');
    $admin_user = get_user_by( 'email', $admin_email );
    if ($admin_user) {
      $username = $admin_user->get('user_login');
    }
    echo $username;
    

    Take into account that once you get the WP_User object ($admin_user) you can retrieve any value from the wp_users table (see here).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search