I’m working a private blog and I’d like to know if this can be done with any other plugin or any other way:
Lets say we have 3 categories: CA, CB and CC, and 3 users, UA, UB and UC, all of them can post but only in a particular category, UA in CA and so on. Now when one of them login to see the blog, this will only shows the post category which he was allowed to for that user.
Any suggestion?
PD: Allow Categories Plugin was supposed to do that but it does not work at all in wp 3.4.2 and it has not been updated long time ago.
This is awesome material for a plugin. This answer will also include how to associate the user with the category (it’s hard to give you a solution without knowing that).
Before we get started, the real meat is in step four (way at the bottom). Skip to that if you don’t need a way to associate categories with users.
So first off, let’s wrap everything up in a class. Our class with a static
init
method that adds actions and such as well as a few class constants.Step One: Add a Field to the User Edit Page
We’ll hook into
edit_user_profile
which will only show when editing other users profiles. You’ll never see it when editing your own profile, but you will see it when editing other users. Our field will grab all the current categories and put them in a drop down menu. We’ll also output a nonce field that we’ll check later on saving the user.The other side of that field is that we need to save it. Do do that, hook into
edit_user_profile_update
. The function will verify the nonce and make sure the current user has permission to edit the user.We now have a way to associate a user with a category.
Step Two: Remove the category meta box
Hook into
add_meta_boxes_post
. If the user is not an admin, remove the category box so authors can’t change category.Step Three: Change the Default Category for Non-Admins
If the user isn’t an admin, they won’t see the category meta box, so we need to make sure that their posts go in the right category. Hook into
pre_option_default_category
to accomplish that.Step Four: Alter the Query
With a way to associate users with a category, we can now alter the query to show only the posts in their category.
Hook into
request
. Check if the user is an admin, and alter the query to include only their category if not.All of the above as a plugin
That’s actually pretty easy with WP on board functions and the »Template Hierarchy«.
You can – depending on your needs – target the
single.php
andcategory-*.php
templates.Toolkit: Needed data
First, we need to
wp_get_current_user()
. Then we can do several checks, depending on whatever you want to check against. For details, do avar_dump( wp_get_current_user() );
and compare your users to see what data is available and what you can check against reliable. Hint: The user ID and theuser_login
are always required and can’t be changed.Lock-Out effect and reroute
Then, you just need to do the check on top of your template, right below the call to
get_header();
for example.You could also redirect him to his author page and list all his allowed categories there.
Alter the query
Another option would be to intercept the query, right before posts get fetched from the DB with a check for the user ID – see details in other questions using
posts_clauses
-filter.Different template for different user
You could also simply intercept the
template_redirect
hook, like explained in this answer.