Obtener datos de usuario registrado en WordPress

To obtain data from a registered user in WordPress, you can follow the steps below:

  1. Use the wp_get_current_user() function to retrieve the current user object. This function returns the WP_User object representing the currently logged-in user.
  2. Check if the user is logged in by calling the is_user_logged_in() function. This function returns a boolean value indicating whether a user is logged in or not.
  3. If the user is logged in, you can access various user data using the WP_User object. Some commonly used methods to retrieve user data include:
  4. get_id(): Returns the user ID.
  5. get_user_login(): Returns the user login name.
  6. get_user_email(): Returns the user email address.
  7. get_display_name(): Returns the user's display name.
  8. get_first_name(): Returns the user's first name.
  9. get_last_name(): Returns the user's last name.
  10. get_avatar_url(): Returns the URL of the user's avatar.
  11. get_roles(): Returns an array of the user's role(s).
  12. You can use these methods to retrieve the desired user information and store it in variables for further use in your code.

Here's an example that demonstrates how to retrieve and display the user's display name:

<?php
if (is_user_logged_in()) {
    $current_user = wp_get_current_user();
    $display_name = $current_user->get_display_name();

    echo "Welcome, " . $display_name . "!";
} else {
    echo "Please log in to view this content.";
}
?>

In this example, we first check if the user is logged in using the is_user_logged_in() function. If the user is logged in, we retrieve the current user object using wp_get_current_user() and then use the get_display_name() method to obtain the user's display name. Finally, we display a welcome message with the user's display name. If the user is not logged in, we display a message asking them to log in.

Please note that these examples assume that you are working within the context of a WordPress theme or plugin.