Family Encyclopedia >> Electronics

How to get WordPress logged in user info for custom results

We recently showed you how to create a personalized experience for your users by letting them save their favorite posts to a personalized library. You can take personalized results to another level by using your first name in places (ie the splash screen). Fortunately, WordPress makes it really easy to get the information of the logged in user. In this article, we will show you how to retrieve information regarding the currently logged in user.

We will use get_currentuserinfo(); function. This could be used anywhere in your theme (header, footer, sidebar, page template, etc). For this to work, the user must be logged in. So we will have to use the is_user_logged_in() conditional statement. Example code:

 

Al registrarte, puedes guardar tus publicaciones favoritas para futuras referencias..

Now, for registered users, we can display a custom message, eg "Hi Syed, everything is here, right where you expect it to be." The above code will become something like this:

 

Hola user_firstname; ?>

Todo está aquí, justo donde esperabas que fuera :)

Al registrarte, puedes guardar tus publicaciones favoritas para futuras referencias..

The magic code we added above is $ current_user-> user_firstname; which is working because the call to get_currentuserinfo() puts the current user information in $current_user . You can use a similar method to get other information about the user, such as their login, user ID, email, website, etc.

Here is a sample usage of all the information:

inicio de sesión de usuario . ""; echo 'Correo electrónico del usuario:'. $ current_user-> user_email. ""; echo 'Nombre del usuario:'. $ current_user-> user_firstname. ""; echo 'Apellido del usuario:'. $ current_user-> user_lastname. ""; echo 'Nombre para mostrar del usuario:'. $ current_user-> display_name. ""; echo 'ID de usuario:'. $ usuario_actual-> ID. ""; ?>

I hope this helps. Combining this with the ability to add favorite posts, you can easily create a personalized experience.