1

I've a Wordpress blog to share content with some people only. Users has to login to access this website.

I created new roles and new capabilities on this blog.

I've got some articles which belongs to a certain category that I would like to share only with people with a certain role.

Is there a hook I could use when somebody request an article, and where I could test if the curent user has the good capabilities, and so modify the request if needed?

1

I think best hook should be 'template_redirect', when this hook is fired main query is already set, so you can look at queried object and if the user has no required capability you can redirect request somewhere:

add_action( 'template_redirect', function() {
  if (
    (
      is_category( 'special-category' )
      || is_singular() && has_category( 'special-category', get_queried_object() )
    ) &&
    ! is_user_logged_in() || current_user_can( 'special-cap' )
  ) {
    wp_safe_redirect( home_url() );
    exit();
  }
} );

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.