skip to Main Content

I am trying to give access of the specific page just for my subscriber user role, if someone else with other then subscriber role or non role user want to access to this page redirect them to a custom login page, I tried with below snippet but I think something is wrong with it, can you please help me to fix this

Example : Restrict access to specific pages

add_action( 'template_redirect', function() {

    // Get global post
    global $post;

    // Prevent access to page with ID 1052
    $page_id = 1052;
    if ( is_page() && ( $post->post_parent == $page_id || is_page( $page_id ) ) ) {

        // Set redirect to true by default
        $redirect = true;

        // If logged in do not redirect
        // You can/should place additional checks here based on user roles or user meta
        if ( current_user_can( 'subscriber' ) || !is_user_logged_in()) {
            $redirect = false;
        }

        // Redirect people without access to login page
        if ( $redirect ) {
            wp_redirect( get_site_url().'/custom-url-login' ); exit;
        }


    }

} );

4

Answers


  1. Wouldn’t this do what you asked for?

    // If user role 'subscriber' & logged in do not redirect
        // You can/should place additional checks here based on user roles or user meta
        if ( current_user_can( 'subscriber' ) && is_user_logged_in()) {
            $redirect = false;
        }
    
    
    Login or Signup to reply.
  2. You could refactor your code like this:

    add_action( 'template_redirect', 'checking_current_user' );
    
    function checking_current_user() 
    {
    
      global $post;
    
      global $current_user;
      
      $page_id = 1052;
    
      if ( 
          ( $post->post_parent == $page_id || is_page( $page_id ) )
          &&  
          ( !in_array('subscriber', $current_user->roles) )
         ) 
      {
        wp_safe_redirect( site_url('/custom-url-login') );
        exit;
      }
      
    }
    

    This answer has been tested and works fine!

    Login or Signup to reply.
  3. First of all you must need to check user is logged-in or not, If user is not logged-in then you need to redirect user to login page, After to login you need to apply code code then it will work fine.

    add_action( 'template_redirect', 'check_current_user' );
    function check_current_user(){
     global $post, $current_user;
    $page_id = 1052;
    if ( is_page() && ( $post->post_parent == $page_id || is_page( $page_id ) ) ) {
       if ( is_user_logged_in() ) {
          /*==Now check user is subscriber==*/
          $current_user = wp_get_current_user();
          $role = $current_user->roles;
          $currentRole = $role[0];
          if($currentRole == "subscriber"){
             $redirect = false;
          }else{
             $redirect = true;
          }
      }else{
         wp_safe_redirect( site_url('/custom-url-login') );
      }
     }
    }
    
    Login or Signup to reply.
  4. WordPress Admin Dashboard > Appearance > Theme File Editor > Theme Functions functions.php

    Edit code by adding to the bottom:

     // Allow subscribers to see Private posts and pages
     $subRole = get_role( 'subscriber' ); 
     $subRole->add_cap( 'read_private_pages' );
     $subRole->add_cap( 'read_private_posts' );
    

    Then change your chosen Pages / Posts from Public to Private visibility:

    Pages > All Pages > Page > Quick Edit > Private (check box)

    You can do a redirection, replace siteurl at the bottom of functions.php using:

    // Redirect to page on login
    function loginRedirect( $redirect_to, $request_redirect_to, $user ) {
        if ( is_a( $user, 'WP_User' ) && $user->has_cap( 'edit_posts' ) === false ) {
            return get_bloginfo( 'siteurl' );
        }
        return $redirect_to; }
    
    add_filter( 'login_redirect', 'loginRedirect', 10, 3 );
    

    or URL redirection to the "page only for subscribers" e.g:

    https://WordPress.com/wp-login.php?user_id=3&redirect_to=https%3A%2F%2Fwordpress.com%2Fabout%2F#top

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search