• Hello,

    I am interested in how I can redirect site viewers to a 404 error page. From the page (Error Message: Sorry, but you do not have permission to view this content.)

    Thanks for your help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Caseproof

    (@caseproof)

    Hi @bartulio

    The content permission feature doesn’t allow redirection when the content is protected. The only way to redirect users from protected content would be to use code snippet similar to the one below:

    add_action( 'template_redirect', function() {
      if ( is_page( array('about', 'activate')) ) {
        $user = wp_get_current_user();
        if( empty($user) || !in_array( 'administrator', (array) $user->roles ) || in_array( 'subscriber', (array) $user->roles ) )  {
          wp_redirect( 'https://cool-domain.com/404' );
          exit;
        }
      }
    });

    Hopefully, that helps.

    Thread Starter bartulio

    (@bartulio)

    The code doesn’t work.
    Unfortunately, the inscription that appears does not suit us. If we could be redirected to a costume page that we create ourselves.
    Do you know of any other alternative?
    Greetings.

    Plugin Author Caseproof

    (@caseproof)

    Could you replace the code from previous message with this one:

    add_action( 'template_redirect', function() {
      // Redirect users from specific page 
      // when user doesn't have particular role 
      $user = wp_get_current_user();
      if ( is_page( array('about', 'activate')) && ( ! is_user_logged_in() || ( ! empty($user) && in_array( 'subscriber', (array) $user->roles ) ) ) ) {
        $redirect = 'https://your-domain.com/wp-login.php'; // Change this to the correct URL
        wp_redirect( $redirect );
        exit;
      }
    } );

    It protects About and Activate pages from not logged in users and those who don’t have Administrator or Subscriber roles.

    I hope that helps.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Redirect to 404 error page’ is closed to new replies.