Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a site in which all categories are open to anyone, except for 2 categories that are password protected: Students, and Teachers. They are open only to specific users types, as follows:

The Students category is open to a user of type student, and to user of type teacher.

The Teachers category is open to a user of type teacher, but not to user of type student.

How do I go about implementing that in Wordpress?

share|improve this question

1 Answer 1

We will use the my_force_login() function that will force the users to login when they press on a specific category.

Wp-login.php

if ( ( empty( $redirect_to ) || $redirect_to == 'wp-admin

After this condition ends write this part

if ($user_name == "student") { wp_safe_redirect("http://www.domain.co.il /?cat=4"); } else { wp_safe_redirect($redirect_to); }

Inside the condition write the username that needs to be redirected to his own category, this is the blocked user that only has one category opened for him.

The domain that is written inside the condition is the redirect address, it should have the category number in it.

functions.php

Add the following function:

function my_force_login() {
global $post;

if (!is_single() && !is_category()) return;
                            global $current_user;
                            get_currentuserinfo();

            if( is_single() ) {
    $categories = wp_get_post_categories($post->ID);
                                            $catid = $categories[0];
                                            $cat = get_category($catid);
                                            $ids = array(2); // array of category IDs that force login to read

                            if(((in_array((int)$cat->cat_ID, $ids)|| (in_array((int)$cat->category_parent, $ids)))&& !is_user_logged_in()) || ((in_array((int)$cat->cat_ID, $ids)|| (in_array((int)$cat->category_parent, $ids))) && is_user_logged_in() && $current_user->user_login  == "student")) {
                            auth_redirect();
                            }
            }
if( is_category(2) ) {
     $cat_ID = get_query_var('cat');

                                            $ids = array(2); // array of category IDs that force login to read


                            if((in_array((int)$cat_ID, $ids) && !is_user_logged_in()) || (in_array((int)$cat_ID, $ids)  && is_user_logged_in() && $current_user->user_login  == "student")) {
                            auth_redirect();
                            }
            }

            if( is_single() ) {
    $categories = wp_get_post_categories($post->ID);

                                            $catid = $categories[0];
                                            $cat = get_category($catid);
                                            $ids = array(4);

}
                            get_currentuserinfo();
                            if((in_array((int)$cat->cat_ID, $ids)|| (in_array((int)$cat->category_parent, $ids)))&& !is_user_logged_in()) {
                            auth_redirect();
                            }
            }
if( is_category(4) ) {
     $cat_ID = get_query_var('cat');

                                            $ids = array(4); 

                            if ( in_array((int)$cat_ID, $ids)  && !is_user_logged_in()) {
                            auth_redirect();
                            }
            }
            }

array(4), is_category(4) contain the category numbers where we would like to force a login in order to block the user from accessing.
The first part of the function (category 2) allows all of the users to enter that category except one user, student in our case. The second part of the function (category 4) allows everyone to enter including student. user_login == "student" defines the blocked user name

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.