Page content only visible to logged in users – WordPress

Do you want to create a page only visible for logged-in users?

In my project I wanted to give access to certain pages to only subscribed users in my WordPress site. I also wanted to show the login form in the page so the users didn't need to access through

Create a Page Template

First things first, let's create a Page Template so we can assign it to any page we want.

Follow the Codex instructions:

Adding the login form

Alright, now that you have your "Private" custom Page template let's edit it.

In order to show the login form in WordPress we can use this little trick:

// Check if user is logged in 
if ( !is_user_logged_in() ){
	echo "Sign in to see the content!";
	wp_login_form( array( 'echo' => true ) );
	// Show content

This snippet will show the login form and, once the user enters the credentials, redirect to the same page showing the content.

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

Leave a Reply

Add <code> Some Code </code> by using this tags.