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