1

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?

  • you should see some sort of error if you have debugging enabled. – Milo Feb 20 '17 at 3:57
  • I do - there's no error, the redirect just... doesn't happen. – CGriffin Feb 20 '17 at 4:10
  • parse_request only fires on pages with a main query. – Milo Feb 20 '17 at 5:18
  • So how can I 100% reliably redirect EVERY page? – CGriffin Feb 20 '17 at 5:22
0

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.

  • No such luck; template_redirect is correctly working for front-end, admin_init is not redirecting on the back-end. – CGriffin Feb 20 '17 at 4:15
  • Actually, I partially retract that: it isn't redirect for the LOGIN page, which it needs to. Once I'm actually ON the back-end, it.... gives me a too many redirects error. I'm sure that's solvable, though. – CGriffin Feb 20 '17 at 4:18
  • Yeah, you are in a redirect loop which you need to solve by examining the page you are on and making sure you really want to redirect it. Regarding login dialog, see the second answer here: wordpress.stackexchange.com/questions/62889/… - which also gives you a hint about using $pagenow in your logical test. – C C Feb 20 '17 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.