Redirect Users who aren’t logged in, aren’t post authors and aren’t admin. Frontend

Question

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;
    }   
}

0
fuxia 3 weeks 2021-10-01T20:02:39-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse