How To Create Members Only Pages in WordPress

I have recently searching a way to restrict some users by accessing specific pages, As usual I have come across with some plugins and as usual I hate them..:p So how did I solve the problem..

This is just a quick method of creating a “members only” section for your site. If you wanted to make an area visible to only those who are logged in, then this is one way you can do that….

The Quick Way

There are many ways you could create a members only section for your site. This is just one quick and dirty way I found. It probably isn’t the most ideal way, but you can use your imaginations to put more use to it if you tried.

Create a custom template

First you will need to create a custom template and to do so you just create a new file, save it as members.php in your theme directory and add this to the top of it.

<?php
/*
Template Name: Members
*/
?>

This will define it as a custom template named Members. You can create custom templates this way for other pages if you like as well.

Next you need to add this, BEFORE anything else.

<?php if ( $user_ID ) : // if logged in ?>

This will check if the user is logged in. After this you can put whatever code you want to show on the page, header, footer, loops, etc.

At the very end of the file, after all that junk above, you need to add this.

<?php else : // if not logged in go to login page
$url = '/wp-login.php';
header("Location:$url");
endif; // end if logged in
?>

What this does is basically redirect the user, if NOT logged in, to the login page. You can change /wp-login.php to whatever page or url of your choice depending on where you want them to be sent. Maybe to the register page for example.

That’s it! Save and upload the file to your theme directory, then when creating a new “Members” page in your admin area, you just choose the Members template from the dropdown box on the right. Test that it works by visiting the page logged in and when logged out.

If you have any questions let me know below.

Leave a Reply