1

Very new to PHP and I'm trying to do the following through a snippets plugin but keep getting critical errors:

  • Restrict all non-logged in users to home, about and pricing pages. If attempting to access other pages, redirect to "no-permission" message page.
  • If user is logged in, allow them to only view posts on the front end if they are the post author. If attempted, redirect to "no-permission" page.
  • Admin has full read access on front end.

I've tried with plugins but nothing quite gives me what I want,

Any help is much appreciated, code below:

add_action( 'template_redirect', 'redirect_non_permitted_users' );

function redirect_non_permitted_users () {
     $userID = get_user_id();
     $authorID = get_the_author_meta(ID);

    if(current_user_can('manage_options')){
 
    }

    elseif( $userID <> $authorID || ! is_user_logged_in() || ! is_front_page() || ! is_page('about') || ! is_page('pricing')){
        wp_redirect('https://www.example.com/no-permission/'); 
    exit;
    }   
}
1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Oct 6, 2021 at 8:26

2 Answers 2

1

get_user_id() is not a function so you'll get an error for that. Additionally, you can get the author id by calling the $post global and accessing the property post_author

add_action( 'template_redirect', 'redirect_non_permitted_users' );

function redirect_non_permitted_users () {
    if (is_admin()) return;
    $userID = get_current_user_id();
    global $post;
    $authorID = absint($post->post_author);
    
    if( $userID !== $authorID && ! is_user_logged_in() && ! is_front_page() && ! is_page('about') && ! is_page('pricing')){
        wp_redirect('https://www.example.com/no-permission/');
        exit;
    }
}
6
  • Many thanks for the reply although it seems to cause some issues. It redirects to the 'no-permissions' page however it throws a 'too many re-directs/can't load page' error. It also throws the same error when accessing the allowed pages such as 'about'/'home'. Also, to get admin access across all pages on the front-end I had to revert to back to the the if/else statement to do nothing if it's the site admin. Oct 2, 2021 at 23:08
  • Updated. I forgot post_author is a string. Added an admin page check, but template redirect shouldn't be called in the admin.
    – Howard E
    Oct 2, 2021 at 23:33
  • Thanks Howard - that certainly fixed some things for the admin. Still issues if it's not the admin. I think I know the issue though, just not too sure how to write the syntax. With "$userID !== $authorID" - would that not cause a loop because the redirect destination was created by the admin? Oct 3, 2021 at 0:22
  • is_admin doesn't mean "is administrator" it means, "in the admin section" - I think your condition is unclear. Clarify the rules for redirection please (in your question.)
    – Howard E
    Oct 3, 2021 at 11:14
  • I've updated the condition.
    – Howard E
    Oct 3, 2021 at 11:30
0

This is seems to do what I want. Issue was $userID conflicting with $authorID on the 'no-permission' page causing a redirect loop.

add_action( 'template_redirect', 'redirect_non_permitted_users' );

function redirect_non_permitted_users () {
  if (is_admin()) return;
  $userID = get_current_user_id();
  global $post;
  $authorID = absint($post->post_author);

  if (is_page('no-permission') || current_user_can('manage_options')){
  return;
  }  

  if( ($userID !== $authorID || ! is_user_logged_in()) && ! 
  is_front_page() && ! is_page('about') && ! is_page('pricing')){
    wp_redirect('https://www.example.com/no-permission/');
    exit;
  }
}

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.