UPDATE: I've done some more research on my problem and have narrowed down the problem further. The question below has been very rewritten from the original.
I'm trying set up a plugin to redirect ALL page requests - front-end, admin, login, etc - to an additional code validation page (whether that's advisable or not is moot, it's what the client wants). Based on the functions, hooks, and filters I need to use, I've determined that the earliest possible hook I can tie the redirect function to is wp
:
add_action('wp', 'cg_check_validation');
The function goes through some $_COOKIE
and $_POST
validation before eventually:
wp_redirect(add_query_arg('validate-access', 1, $_SERVER['REQUEST_URI']));
The following filter and action were set up to handle the redirection:
add_filter('query_vars', 'cg_redirect_query_vars');
function cg_redirect_query_vars($query_vars)
{
$query_vars[] = 'validate-access';
return $query_vars;
}
add_action('parse_request', 'cg_redirect_parse_request');
function cg_redirect_parse_request(&$wp)
{
// if our custom access validation query var exists, load our custom plugin view
// then exit the script
if (array_key_exists('validate-access', $wp->query_vars)) {
include(plugin_dir_path(__FILE__) . 'views/validate-access.php');
exit();
}
return;
}
The redirect and code validation is working for ALMOST every page, however the login page and the base admin page (/wp-admin/
) are not being redirected at all. What would be causing these pages to be exempt?
EDIT: Another update! The redirect is ONLY working on admin pages that have query strings. So, for example, the redirect works on wp-admin/edit.php?post_type=page
, but not /wp-admin/edit-comments.php
. Though having identified the problem, I'm no closer to solving it -- my best guess is that parse_request
or query_vars
isn't fired on pages without query strings?
UPDATE: I've done some more research on my problem and have narrowed down the problem further. The question below has been very rewritten from the original.
I'm trying set up a plugin to redirect ALL page requests - front-end, admin, login, etc - to an additional code validation page (whether that's advisable or not is moot, it's what the client wants). Based on the functions, hooks, and filters I need to use, I've determined that the earliest possible hook I can tie the redirect function to is wp
:
add_action('wp', 'cg_check_validation');
The function goes through some $_COOKIE
and $_POST
validation before eventually:
wp_redirect(add_query_arg('validate-access', 1, $_SERVER['REQUEST_URI']));
The following filter and action were set up to handle the redirection:
add_filter('query_vars', 'cg_redirect_query_vars');
function cg_redirect_query_vars($query_vars)
{
$query_vars[] = 'validate-access';
return $query_vars;
}
add_action('parse_request', 'cg_redirect_parse_request');
function cg_redirect_parse_request(&$wp)
{
// if our custom access validation query var exists, load our custom plugin view
// then exit the script
if (array_key_exists('validate-access', $wp->query_vars)) {
include(plugin_dir_path(__FILE__) . 'views/validate-access.php');
exit();
}
return;
}
The redirect and code validation is working for ALMOST every page, however the login page and the base admin page (/wp-admin/
) are not being redirected at all. What would be causing these pages to be exempt?
EDIT: Another update! The redirect is ONLY working on admin pages that have query strings. So, for example, the redirect works on wp-admin/edit.php?post_type=page
, but not /wp-admin/edit-comments.php
. Though having identified the problem, I'm no closer to solving it -- my best guess is that parse_request
or query_vars
isn't fired on pages without query strings?
Hooking to admin_init
is what you want, for doing a redirect while inside admin. So you'd need both - that one, and the hook to template_redirect
for the front-end handler.
parse_request
only fires on pages with a main query. – Milo Commented Feb 20, 2017 at 5:18