2

I'm building a site for some army guys to share photos, and one thing keeps stumping me.

They have some photos that they'd like everyone who is signed up for the site to view, and others that just they can view. But they can't be administrators.

The only help I can find is on only allowing certain user levels to POST to a category. I would like to basically restrict 'subscribers' from VIEWING some posts.

Not asking anyone to write something for me, but a point in the right direction would be awesome.

  • You could combine wordpress Roles and Capabilities with php if else statement to achieve it. Go here codex.wordpress.org/Roles_and_Capabilities and also don't forget to go the bottom of that page to read about current_user_can() and user_can() functions. – Ari Sep 9 '13 at 15:03
5

If I understand you have some categories, eg: 'reserved', 'people', 'landscapes', 'personal' and so on.

Now you want that subscribers can see posts in, e.g. 'people', 'landscapes' categories but not posts in 'reserved' and 'personal' categories.

This is relatively easy, just hook into pre_get_posts and if the request is for a post having that terms or the term archive disable the viewing.

To disable you have different chanches, you can redirect, you can show 404 or you can show a custom template.

Below I'm sayng how to last option.

First of all create a template file, e.g. not-allowed.php and put in your theme folder.

Then in functions.php use this code:

add_filter('template_include', 'restict_by_category');

function check_user() {
  $user = wp_get_current_user();
  if ( ! $user->ID || in_array('subscriber', $user->roles) ) {
    // user is not logged or is a subscriber
    return false;
  }
  return true;
}

function restict_by_category( $template ) {
  if ( ! is_main_query() ) return $template; // only affect main query.
  $allow = true;
  $private_categories = array('reserved', 'personal'); // categories subscribers cannot see
  if ( is_single() ) {
    $cats = wp_get_object_terms( get_queried_object()->ID, 'category', array('fields' => 'slugs') ); // get the categories associated to the required post
    if ( array_intersect( $private_categories, $cats ) ) {
      // post has a reserved category, let's check user
      $allow = check_user();
    }
  } elseif ( is_tax('category', $private_categories) ) {
    // the archive for one of private categories is required, let's check user
    $allow = check_user();
  }
  // if allowed include the required template, otherwise include the 'not-allowed' one
  return $allow ? $template : get_template_directory() . '/not-allowed.php';
}
  • I'm getting a fatal error referring to this this line ' $cats = wp_get_objects_terms( get_queried_object()->ID, 'category', array('fields' => 'slugs') );' – OneEightLeft Sep 10 '13 at 17:12
  • It's simply a typo. Tight function name is wp_get_object_terms I've fixed code in answer. @OneEightLeft – gmazzap Sep 11 '13 at 0:17
  • That is great. It really helps a lot. Is it possible to limit categories so that user in subscriber group does not see this posts at all? Not even in a list of posts? – Kiki Jan 23 at 9:58
  • And also i have category master, and subcategory child, is it possible to also limit all categories that ares child categories of categorie that is listed in $private_categories ? – Kiki Jan 23 at 10:04
0

Download and install the plug-in Restrict Categories. Restrict certain categories for certain roles and post the photos in the appropriate categories.

  • 1
    Posting links offsite by itself is frowned upon and not a good answer, can you explain how the plugin works, and how it implements what the asker wants at a code level? All it takes is for that URL to move or break and your question becomes useless – Tom J Nowell Mar 22 '14 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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