php - Where do I implement this display of User Meta Data, and how to put it in a table?

admin2025-06-02  1

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.

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.

Share Improve this question asked Mar 4, 2019 at 22:18 Ali MosallaeiAli Mosallaei 53 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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]

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748843048a314183.html

最新回复(0)