One of the core feature of WordPress has always been to allow people to easily register on your blog. You can use this function to filter who can comment on your posts for example or to allow other people to submit articles to your blog.
But so far there is nothing that allow you to restrict access to a part of your blog to registered members, apart from the password you can add on a single post/page.
Some solutions have been suggested in the past and we are trying here to compile the best of those to make the ultimate members page.
- Content shouldn’t be accessible from non logged users.
- Provide an easy way to login, without going to the default WordPress backend
- Allow people to post articles to be reviewed
- Protect Rss feed, archives, index, etc from displaying restricted posts
- Allow admin to use subcategories on the Members posts
- Users won’t be able to set the category of their post themselves, which in a way is a good thing. Each submitted post will go to the default ‘Members’ category. The admin can then add subcategories if needed.
- You need a category.php file in your theme, might change in the future though
The code will pretty much all go in the function.php file as a php class. We are gonna use WordPress categories here, no page with custom template. That’s the way we started it but it seems more flexible using the default categorisation rather than a custom page template, especially for sub-categories.
This tutorial is for people with some knowledge in php. You might pull it off by raw copy-paste but it’s always better to understand what you are doing in case something goes wrong. And as usual do this at your how risks, it worked for us on test sites, but you never know…backup backup backup!
For the lazy here is everything you need in two files: one to include in your function.php and the category.php template you need: members.zip
So should we get started?
This tutorial is based on the fact that your members only category is called ‘Members’. If you want to change the name to something else you will have to make sure every reference to ‘members’ as the category is changed.
This is the function that will hide the members category from archives, general Rss feed, index. It also redirects someone not logged and trying to access the members rss to the Members category page with the log in form. We have a small issue here but we’ll talk about it later.
This is a quick function that will find out if a post’s assigned categories are descendants of target categories.
Members category template
Next we need this special template for the members category. The template will go into an other function in our class and this function will be called in the category.php file.
The comments should hopefully be enough to let you understand what is happening. Basically if someone not logged in he will see the login form, if he is he will see a post submission form, a logout button and the latest post in the category.
Now we need to change the category.php file to call this function if we are in the right category:
Just a quick cosmetic fix on the textarea in the form. Adding some style capabilities will make it easier for users to structure their posts.
All in all
Now we have all our function we need to wrap them up in an object and hook them in the different places. So the full code, without the category.php modification of course, looks like this:
The hooks are in the constructor of the class and get called when we create the object (last line). This class goes in a separate file that then gets included in your function.php file or straight in the function.php, that’s up to you.
There are some issues we couldn’t resolve. The main one is about the rss of the members category. The way Firefox caches rss feeds make the solution not perfect. If you are not logged in you will be redirected, no problem, but then if you login and go back to the rss feed, the browser cache will kick in and you will get redirected again.
The other small issue is in TinyMCE, the style dropdown we’ve added has got a strange default value. Not a big deal but still could be better.
If anyone has an idea about those to problem feel free to discuss in the comment section. And of course the discussion is open for comment, questions and anything related to the above.