Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
Coppa WPB
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Come ottenere le informazioni sull’utente connesso in WordPress per ottenere risultati personalizzati

Nota editoriale: guadagniamo una commissione dai link dei partner su WPBeginner. Le commissioni non influenzano le opinioni o le valutazioni dei nostri redattori. Per saperne di più su Processo editoriale.

Recentemente vi abbiamo mostrato come creare un’esperienza personalizzata per i vostri utenti, permettendo loro di salvare i loro articoli preferiti in una libreria personalizzata. È possibile portare i risultati personalizzati a un altro livello, utilizzando il loro nome di battesimo in alcuni punti (ad esempio nello schermo di benvenuto). Fortunatamente, WordPress rende molto semplice ottenere le informazioni dell’utente connesso. In questo articolo vi mostreremo come recuperare le informazioni relative all’utente attualmente connesso.

Utilizzeremo la funzione get_currentuserinfo();. Questa funzione può essere utilizzata in qualsiasi punto del tema (header, footer, barra laterale, template della pagina ecc.). Affinché funzioni, l’utente deve essere connesso. Pertanto, sarà necessario utilizzare la dichiarazione condizionale is_user_logged_in(). Esempio di codice:

<?php if ( is_user_logged_in() ) { ?>
    <!-- text that logged in users will see -->
<?php } else {   ?>
    <!-- here is a paragraph that is shown to anyone not logged in -->

<p>By <a href="<?php bloginfo('url'); ?>/wp-register.php">registering</a>, you can save your favorite posts for future reference.</p>
<?php } ?>

Ora, per gli utenti che hanno effettuato il login, possiamo mostrare un messaggio personalizzato, ad esempio: “Ehi Syed, è tutto qui, proprio dove speravi che fosse”. Il codice precedente si trasformerà in qualcosa di simile:

<?php if ( is_user_logged_in() ) { ?>
    <!-- text that logged in users will see -->

<?php global $current_user; get_currentuserinfo(); ?>

<h1>Hi <?php echo $current_user->user_firstname; ?></h1>

<p>Everything is here, right where you hoped it would be :)</p>

<?php } else {   ?>
    <!-- here is a paragraph that is shown to anyone not logged in -->

<p>By <a href="<?php bloginfo('url'); ?>/wp-register.php">registering</a>, you can save your favorite posts for future reference.</p>
<?php } ?>

Il codice magico che abbiamo aggiunto sopra è $current_user->user_firstname; il che funziona perché la chiamata a get_currentuserinfo() inserisce le informazioni dell’utente corrente in $current_user. Si può usare un metodo simile per ottenere altre informazioni su chi siamo, come l’accesso, l’ID utente, l’email, il sito web, ecc.

Ecco un esempio di utilizzo di tutte le informazioni:

<?php global $current_user;
      get_currentuserinfo();

      echo 'Username: ' . $current_user->user_login . "
";
      echo 'User email: ' . $current_user->user_email . "
";
      echo 'User first name: ' . $current_user->user_firstname . "
";
      echo 'User last name: ' . $current_user->user_lastname . "
";
      echo 'User display name: ' . $current_user->display_name . "
";
      echo 'User ID: ' . $current_user->ID . "
";
?>

Spero che questo sia d’aiuto. Combinando questa funzione con la possibilità di aggiungere articoli preferiti, è possibile creare facilmente un’esperienza personalizzata.

Divulgazione: I nostri contenuti sono sostenuti dai lettori. Ciò significa che se cliccate su alcuni dei nostri link, potremmo guadagnare una commissione. Vedi come WPBeginner è finanziato , perché è importante e come puoi sostenerci. Ecco il nostro processo editoriale .

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Il kit di strumenti WordPress definitivo

Ottenete l'accesso gratuito al nostro kit di strumenti - una raccolta di prodotti e risorse relative a WordPress che ogni professionista dovrebbe avere!

Reader Interactions

10 commentiLascia una risposta

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. S Meredith says

    Hi Syed,
    Fantastic site.
    It’s helped me a lot with understanding WP and editing my own site.
    My question is, once I’ve inserted the above function into my child themes functions.php, how would i create a shortcode so that I can then display this user information on a generic page?

    Are shortcodes the best way to allow this to be displayed on any page i’d like.

    For example, I have a static page that I would like to display ‘Welcome {user_name}’ and their profile picture.

  3. Pavan says

    I know nothing about php. I just want to show the username of my customer on TOP bar navgation, and from there he can access his account. How do I do that.

  4. Faruk says

    What if the user not logged in. Can the wordpress still send personalised email with his name? and if sa what is the variable and how?

Lascia una risposta

Grazie per aver scelto di lasciare un commento. Tenga presente che tutti i commenti sono moderati in base alle nostre politica dei commenti e il suo indirizzo e-mail NON sarà pubblicato. Si prega di NON utilizzare parole chiave nel campo del nome. Avremo una conversazione personale e significativa.