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.

1
  • 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, 2013 at 15:03

2 Answers 2

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';
}
4
  • I'm getting a fatal error referring to this this line ' $cats = wp_get_objects_terms( get_queried_object()->ID, 'category', array('fields' => 'slugs') );' Sep 10, 2013 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, 2013 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, 2019 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, 2019 at 10:04
1

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

2
  • 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, 2014 at 15:21
  • ...In general, posting links here is frowned upon as a convention. Then also, in general, you only break convention when there is a good reason to. However, this is a perfect circumstance to do so. Thank you for sharing your suggestion. This is neither your code or work you have produced in any way. No need to duplicate all or any of the information at the other end of the link. --And no amount of your unnecessary expansion would help if the link goes dead. Mods love to close topics and dump on people. You did good, man. The only detail missing is the publisher, Matthew Muro. Aug 16, 2023 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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