$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Where do I implement this display of User Meta Data, and how to put it in a table?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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]

Post a comment

comment list (0)

  1. No comments so far