Search code examples
phpwordpresstemplate-redirect

How To Restrict Access To Custom WordPress Template


I have a custom template that I built which manages a separate table inside a WordPress database. I have had success restricting access unless logged in on all other pages (including existing custom templates) accept my custom template (page id 9597) using this code:

add_action( 'template_redirect', 'add_restrict_access');
  function add_restrict_access(){

  if( ! is_user_logged_in() && is_page( 9597 ) ) {
    wp_redirect( '/wp-login.php' );
    exit;
  }
}

If I change the is_page number to any other page ID, the redirect works. Anyone have any ideas?


Solution

  • SSL cert issue. Page was viewable without SSL, as soon as I added https it started working properly. Weird.