18

How To Create WordPress Members Only Pages

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....
How To Create WordPress Members Only Pages

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.

Shortlink:
Share This Post

Get automatic updates! Subscribe to Our RSS Feed or Get Email Updates sent straight to your inbox!

About the Author

Jared is from Boston working as a web and graphic designer. Also owns the design blog Tweeaks.com, and has designed many other websites powered by Wordpress including the New2WP theme.

Level: Featured, Noob

User Comments

( ADD YOURS )

  1. Thanks so much for this...I coupled your terrific suggestion with the Register Plus plugin (http://wordpress.org/extend/plugins/register-plus/) and the combo works like a charm.

    Now I can create a secure members area with all kinds of customization and functionality. You're a lifesaver!


  2. Glad to be of some help :)


  3. Thanks for this post. You made it simple for me to understand


  4. Hi
    thanx for the pointers, this works like a dream.

    Except ... after logging in, the user gets directed to his profil-page.
    This is not really what I want. He should get redirected to the page he tried to access in the first place.

    What am I missing?


  5. Awesome! Simple and effective.
    Thanks for sharing!


  6. Anytime :)


  7. Hi,
    I'm trying to get this to work and getting a message saying "headers already sent". What I was doing was setting up a members.php page that has all the code you note above, wrapped around the contents of the page.php file.

    Can you tell me what I'm doing wrong or show a sample of a page.php that would work? I'm guessing their's a conflict with a get_headers() call, but that needs to be there.


  8. Actually, you can delete that question. It looks like my cutting and pasting added an extra space that was getting output and goofing everything up. I think all is good now. Thanks!


  9. THis is great, it is one piece of a puzzle I have to solve... I, like a couple of others who made comments need to do have the user redirected back to the original page they were trying to see, they should not be logged into the dashboard of the site, but have to put in a password to see a page and all it's sub pages.

    Has anyone else done something similar? How did the other commenters redirect back to the original page? I appreciate any help and I also think the way you wrote up this is very easy to follow! Thanks


  10. @ Helen

    If you want to redirect all users to that log in to a specific destination, you can use this plugin: http://wordpress.org/extend/plugins/peters-login-redirect/

    I have a secure area that only registered users could access, so I created a rule that basically said, if user type is 'subscriber', redirect upon login to the secure area page. This rule doesn't apply to the user type 'admin' so I still see the dashboard when i log in.

    Hope that helps...it's worked great for me!


  11. @Helen You can do that by using the auth_redirect() function in a page that requires users to be logged in before they can view it.

    Here is an alternative way of doing what I show you in this post.

    You can put this in your functions.php file which says if not currently on a single post. Creates a list of post IDs you want to restrict.

    Then it checks if the page trying be viewed has an ID you specify, AND the user is not logged in, then go to login page.

    The auth_redirect will send the HTTP_REFERRER information to the login form which is used in sending you back to the referring page.

    function you_must_login() {
    	global $post;
    
    	if ( !is_single() ) // if not currently on a single post (can use any other conditional here of your choice)
    	return;
    
    	$post_ids = array( 188, 185, 171 ); // array of post IDs that force users to login before reading
    
    	if ( in_array( (int) $post->ID, $post_ids ) && !is_user_logged_in() ) {
    		auth_redirect();
    	}
    }
    

    Then add this to the very top of whichever template file of the page or you want to restrict before the_header() call

    <?php you_must_login(); ?>
    


  12. @Tom so did the register plus plugin you mention in the first comment end up not working out?

    or is that a new plugin, the peters login redirect?


  13. It's another plugin for another layer of functionality (just redirecting users to pre-determined location based on user type)


  14. Ah I see. Well, it might be better to use a plugin for this, for anyone who isn't extremely interested in learning how to do everything by hand like myself. I would rather make something work without the use of a plugin, more control over it.


  15. @jared - I totally hear you. I need to get more proficient and less lazy! ;)


  16. Yea it can be difficult doing that. The whole thing which drives me to be as proficient as I can, and do anything and everything within all that WordPress can do, is this very site.

    When I get an idea to post about here, I may or may not know how to do it already myself. Usually I don't, or not entirely, or do know it quite well but I want know more, I then learn things I never knew, or wouldn't learn normally in the process of writing it, since I try to get all the facts and correct things before passing it along so I therefore get deeper into it.

    Sure there's plugins for making members only sites. Probably about 1000 of them too, but I want to do that myself, and build it into the theme itself.
    This takes a deeper understanding most of the time than just making a plugin, too I've learned.


  17. Nice tip, thanks Jared.

  1. Avatar

    Your Name
    September 8


    CommentLuv Enabled