0

So I have the code below that should pull the user metadata of the currently logged-in user.

<?php
$current_user = wp_get_current_user();
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 />';
?> 

Now the issue is that I do not know where to put this code (I know, embarrassing). I can't put it on a page, because then the PHP code would show and wouldn't change into displaying anything. I thought of putting it in functions.php, however there is no short code associated with this...

Second question I have is how can I implement this into a table? So let's say USERNAME is a row, and next to that, it should show the current user's username. I know how to code a table in Wordpress, I just am unaware of how to implement my code into it.

Any help would be appreciated!

Thanks.

0

If I understood your question correctly, you want to display this data on a page on front-end. In this case you can either create a page template in your theme folder OR create a shortcode in functions.php. Here is a quick shortcode that may help.

add_shortcode('currentuser', 'shortcode_current_user');

function shortcode_current_user($atts){
   ob_start();
   $current_user = wp_get_current_user();
   echo '<table>';
       echo '<tr><td>Username: </td>' . '<td>'.$current_user->user_login . '</td></tr>';
       echo '<tr><td>User email: ' .'<td>'. $current_user->user_email . '</td></tr>';
       echo '<tr><td>User first name: ' . '<td>'.$current_user->user_firstname . '</td></tr>';
       echo '<tr><td>User last name: ' .'<td>'. $current_user->user_lastname . '</td></tr>';
       echo '<tr><td>User display name: ' .'<td>'. $current_user->display_name . '</td></tr>';
       echo '<tr><td>User ID: ' .'<td>'. $current_user->ID . '</td></tr>';
   echo '</table>';

   return ob_get_clean();
}

Now it can be used on any page/post/widget to display user data by putting [currentuser]

  • Thank you! This is exactly what I was looking for! – Ali Mosallaei Mar 5 at 0:29

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.