plugin development - wp_redirect on base wp-admin and login

admin2025-01-07  3

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?

Share Improve this question edited Feb 20, 2017 at 4:35 CGriffin asked Feb 20, 2017 at 3:40 CGriffinCGriffin 1316 bronze badges 4
  • you should see some sort of error if you have debugging enabled. – Milo Commented Feb 20, 2017 at 3:57
  • I do - there's no error, the redirect just... doesn't happen. – CGriffin Commented Feb 20, 2017 at 4:10
  • parse_request only fires on pages with a main query. – Milo Commented Feb 20, 2017 at 5:18
  • So how can I 100% reliably redirect EVERY page? – CGriffin Commented Feb 20, 2017 at 5:22
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262189a799.html

最新回复(0)