Restrict Content Based On Username

On a recent project, I had to show specific posts & content for the correlated user.
I used Advanced Custom Fields to set a client name field which I would manually enter to match to the login name for each user that I create.
I am sure there are more efficient ways to accomplish this, possibly using wp_dropdown_users to create a drop down of users and selecting from that. In my instance, I had already configured a custom post type for clients that had a custom field for client name, so it made sense for me to work off of that.


  // sets ACF field as variable
$client_name = get_field('client_name');

  // sets current user login name as variable
$current_user = $current_user->user_login;

  // checks if current user is equal to client name
if ( $current_user == $client_name ) { 
      //Content to show if current user is equal to username
      <p> Some Content Goes Here </p>
 } 

else {   
      // Content to show other users
      <p> Other Content Goes Here </p>
}

Leave a Reply

Your email address will not be published. Required fields are marked *