Add Snippet To Project
Are you looking to create a more personalized and interactive experience for users on your WordPress website? One way to achieve this is by displaying user information. By showing specific details about your users, such as their name, profile picture, or even their activity history, you can enhance user engagement and make them feel more connected to your site. In this article, we will explore different methods and tools that can help you display user info on your WordPress website. Whether you want to showcase user profiles, create a members directory, or customize the user login page, we’ve got you covered. So, let’s dive in and start leveraging the power of user information in WordPress.
function wpturbo_display_user_info($user_id) {
$user_data = get_userdata($user_id);
?>
<div class="user-info">
<h2><?php echo $user_data->display_name; ?></h2>
<p><?php echo $user_data->user_email; ?></p>
<p><?php echo $user_data->user_registered; ?></p>
</div>
<?php
}
add_action('wp_footer', 'wpturbo_display_user_info');
The code snippet provided is for displaying user information on a WordPress website. This function, called wpturbo_display_user_info()
, takes a parameter $user_id
which represents the ID of the user for whom the information needs to be displayed.
The first line of the function uses the get_userdata()
function to retrieve all the data associated with the specified user ID. This function returns an object with various properties that represent different user data such as display name, email, registered date, and so on. We store this object in the variable $user_data
.
Next, we create a <div>
element with a class of "user-info". Inside the <div>
, we display the user's display name, email, and registration date using <h2>
, <p>
, and <p>
tags, respectively. We access the properties of the $user_data
object and display them using echo
, which outputs the corresponding values to the webpage.
<div class="user-info">
<h2><?php echo $user_data->display_name; ?></h2>
<p><?php echo $user_data->user_email; ?></p>
<p><?php echo $user_data->user_registered; ?></p>
</div>
To customize the HTML structure or styling of the user information, you can modify the markup within the <div>
according to your requirements. You can add additional information or change the HTML tags used.
Finally, we hook the wpturbo_display_user_info()
function to the wp_footer
action using the add_action()
function. This ensures that the user information is displayed in the footer of every page on the WordPress website.
add_action('wp_footer', 'wpturbo_display_user_info');