Skip to content

Page Templates & Authentication Guards

Xe Plugin allows you to register custom virtual page templates directly from the plugin and enforce authentication rules (protecting member pages or redirecting logged-in users away from guest pages).


Defining Page Templates

Page templates are configured in src/PageTemplates.php:

public function all(): array {

  $page_templates = XE_PLUGIN_PATH . 'page-templates';

  return [
    'xep-login' => [
      'title'      => esc_html__( 'Login', 'xe-plugin' ),
      'path'       => $page_templates . '/login.php',
      'single_use' => true,
      'auth'       => false // Guest only
    ],
    'xep-signup' => [
      'title'      => esc_html__( 'Signup', 'xe-plugin' ),
      'path'       => $page_templates . '/signup.php',
      'single_use' => true,
      'auth'       => false // Guest only
    ],
    'xep-dashboard' => [
      'title'      => esc_html__( 'Dashboard', 'xe-plugin' ),
      'path'       => $page_templates . '/dashboard.php',
      'single_use' => true,
      'auth'       => true  // Authenticated users only
    ],
  ];

}

Template Configuration Options:

  • title: The name shown in the WordPress Page Template dropdown.
  • path: Absolute path to the template file.
  • single_use: When true, hides the template from the dropdown if another published page is already using it.
  • auth:
  • true: Requires user to be logged in.
  • false: Accessible only to guests (logged-in users are redirected away).

Authentication Protection & Redirects

Authentication guards are enforced automatically in src/Setup.php via template_redirect:

1. Protected Pages (require_authentication)

If an unauthenticated visitor accesses a page assigned a template where 'auth' => true (like Dashboard), they are automatically redirected to the Login page (xep-login):

// Handled automatically in Setup.php
public function require_authentication(): void {
  // Checks if template requires auth and redirects to login page if guest
}

2. Guest-Only Pages (redirect_authenticated)

If a logged-in user visits a guest-only page (like Login, Signup, or Forgot Password), they are automatically redirected to the Dashboard page (xep-dashboard).


Template Utility Methods

Find Page ID Using a Template:

// Get the ID of the page assigned the 'xep-login' template
$login_page_id = _xe_plugin()->templates()->get_page_id( 'xep-login' );
$login_url     = get_permalink( $login_page_id );

Check if Current Page Uses a Template:

if ( _xe_plugin()->templates()->is_current( 'xep-dashboard' ) ) {
  // Current page is the dashboard template
}