• Hi everyone,

    For the last few hours, i’ve been searching on how to display the current user info in a table, but without success. I know it’s not hard probably to do it, but i’m pretty new to coding and honestly, i’m stuck.
    Would really appreciate if you guys can help me figure it out.

    So far, i can pull some info from the database and display it using:

    <?php
        $current_user = wp_get_current_user();
        /**
         * @example Safe usage: $current_user = wp_get_current_user();
         * if ( !($current_user instanceof WP_User) )
         *     return;
         */
        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 />';
    ?>

    I would like to be able to show this into a table like:

    Username Email First name Last name
    User01 example@mail.com John Doe

    Thanks again for your help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • This could be helpful, add to your functions.php file:

    function current_user_info() {
    	global $current_user;
    	if(!is_user_logged_in()) return false;
    	$name = $current_user->user_login;
    	$email = $current_user->user_email;
    	ob_start();
    	?>
    	<table>
    		<tr>
    			<td>Username</td>
    			<td>Email</td>
    		</tr>
    		<tr>
    			<td><?php echo $name;?></td>
    			<td><?php echo $email;?></td>
    		</tr>
    	</table>
    	<?php
    	return ob_get_clean();
    }
    add_shortcode('current-user-info','current_user_info');

    Usage:
    Add [current-user-info] (shortcode) where you want the table to be displayed.

    Note: for the first name and last name, I didn’t include them as I didn’t know their meta or how you are implementing that..

    Like this:

    <?php /* TOP PART OF PHP HERE */ ?>
    <table id="my_id"><!-- USE YOUR ID AS NEEDED TO STYLE -->
    <tr>
    <th colspan="6">TABLE NAME HERE</th>
    </tr>
    <tr>
    <th>User Name</th>
    <th>Email</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Display Name</th>
    <th>User ID</th>
    </tr><!-- Then just add a row with the td's that echo what should be in each column -->
    <tr>
    <td><?php echo $current_user->user_login; ?></td>
    <td><?php /* ADD NEXT ONE AND THEN NEXT ONE ETC */ ?></td>
    <!-- ADD THE OTHER TD'S -->
    </tr>
    </table>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display current user info in a table.’ is closed to new replies.