Here’s the updated code with the explanation and last two lines added:
Updated Code with Last Two Lines
add_action('query_vars', 'add_query_vars');
function add_query_vars($vars) {
$vars[] = 'verify'; // Add 'verify' as a query variable
return $vars;
}
add_action('init', 'verify_add_rewrite_rule');
function verify_add_rewrite_rule() {
// Use ^verify/?$ to strictly match "verify" and ensure it ends there
add_rewrite_rule('^verify/?$', 'index.php?verify=1&post_type=page', 'top');
}
add_filter('template_include', 'template_include_verify', 1000, 1);
function template_include_verify($template) {
if (get_query_var('verify')) {
$new_template = get_template_directory() . '/verify.php';
if (file_exists($new_template)) {
$template = $new_template;
}
}
return $template;
}
Additional Notes:
Rewrite Rule Strict Matching: This ensures that only /verify/
(with or without a trailing slash) will match. No partial paths like /verifxx/
or /verify-xxx/
will be routed to your template.
Flush Rewrite Rules: After updating the code, navigate to Settings > Permalinks in WordPress and click “Save Changes” to flush the rewrite rules.