Virtual Rewrite Endpoints
Xe Plugin provides a built-in virtual URL routing system using WordPress rewrite endpoints. This allows you to render custom frontend interfaces, dashboards, or full web application layouts without needing to create static WordPress pages.
How It Works
The routing system is managed in src/Endpoints.php:
- Registers Rewrite Endpoints: Calls
add_rewrite_endpoint( $slug, EP_ROOT | EP_PAGES )oninit. - Registers Query Vars: Whitelists endpoint slugs via the
query_varsfilter. - Template Redirect: Intercepts requests matching the endpoint on
template_redirectand loads the corresponding PHP template file withload_template().
Defining Endpoints
Endpoints are configured in the all() method of src/Endpoints.php:
public function all(): array {
$templates_dir = XE_PLUGIN_PATH . 'templates';
$index_file = XE_PLUGIN_PATH . 'templates/index.php';
return [
'xep-login' => [
'path' => $templates_dir . '/login.php',
'slug' => 'login',
'auth' => false // Guest only
],
'xep-dashboard' => [
'path' => $templates_dir . '/dashboard.php',
'slug' => 'dashboard',
'auth' => true // Authenticated users only
],
'xep-sales' => [
'path' => $index_file,
'slug' => 'sales',
'auth' => true,
'titles' => [
'trash' => esc_html__( 'Deleted Sales', 'xe-plugin' ),
'add-new' => esc_html__( 'Add New Sale', 'xe-plugin' ),
'edit' => esc_html__( 'Edit Sale', 'xe-plugin' ),
'all' => esc_html__( 'Sales', 'xe-plugin' ),
]
],
];
}
URL Helpers & Routing API
Generating URLs:
// Get base endpoint URL: https://example.com/sales/
$sales_url = _xe_plugin()->endpoints()->get_url_by_key( 'xep-sales' );
// Get nested subpath URL: https://example.com/sales/edit/42/
$edit_url = _xe_plugin()->endpoints()->get_url_by_key( 'xep-sales', 'edit/42' );
// Get the current endpoint's URL
$current_url = _xe_plugin()->endpoints()->get_current_url();
Checking Current Endpoint:
// Check if currently on a specific endpoint
if ( _xe_plugin()->endpoints()->is_current( 'xep-dashboard' ) ) {
// Logic for dashboard...
}
// Get the active endpoint key (e.g. 'xep-sales')
$active_key = _xe_plugin()->endpoints()->get_key();
// Get the active endpoint slug (e.g. 'sales')
$active_slug = _xe_plugin()->endpoints()->get_slug();
Subpath Parsing (Action & ID)
Xe Plugin parses subpath parameters automatically. For example, if a user visits https://example.com/sales/edit/15:
// Get the subpath string (e.g. "edit/15")
$subpath = _xe_plugin()->endpoints()->get_subpath();
// Get the primary action: "edit"
$action = _xe_plugin()->endpoints()->get_action();
// Get the numerical record ID: 15
$id = _xe_plugin()->endpoints()->get_id();
Contextual Page Titles:
// Returns "Edit Sale" when $action === 'edit'
$title = _xe_plugin()->endpoints()->get_title( $action );
Flushing Rewrite Rules
Whenever you add or change endpoint slugs, remember to flush WordPress rewrite rules by visiting Settings > Permalinks in the WordPress Admin or by triggering flush_rewrite_rules().