I need to redirect the page based on the query vars in my plugin admin menu. I don't know where I can hook in to read the query vars or the GET vars order to decide if the page needs to be redirected.

  • You are in the backend, but I got confused when you mentioned checking permalinks there? Can you elaborate? Thanks – birgire Apr 2 '14 at 3:40
  • Sorry, I updated it. I meant the GET vars. – George Kendros Apr 2 '14 at 12:46

You can check the $current_screen on the current_screen action, and redirect after checking some GET conditions:

function wpd_current_screen( $current_screen ) {
    if ( 'toplevel_page_your_menu_item_id' == $current_screen->base ) {
        // check some condition and
        wp_redirect( admin_url() );
add_action( 'current_screen', 'wpd_current_screen' );
  • This fires early enough but I don't seem able to access the GET variables (or the query vars since I guess it would be too early to access GET). Reading your answer I realize my initial question was clear enough - I wanted to access the 'page' variable as well as an additional GET variable (which I set with add_query_arg). I'm guessing there's no way to get that. – George Kendros Apr 2 '14 at 12:44
  • I played around with it a bit more and I was able to use this hook as desired. Instead of reading anything inside $current_screen (which won't work because there are additional variables I need) I'm running add_query_arg() and adding an arbitrary variable. add_query_arg() returns the relative url with all variables so I can parse out the stuff I need. This is obviously a dirty method (adding arbitrary query args just to return the URL) so hopefully I can find a more elegant solution to that part. But the end result is the same, I'm able to parse out my variables early enough for a redirect. – George Kendros Apr 2 '14 at 15:22

$_GET[] query variables are passed via the URL request given to the web server, and are available at any time in the WordPress action cycle. You do not need to hook into an action to have access to GET variables.

The "init" hook is generally the best action to hook into for something like this, unless it's important to act before any other plugins, and will allow you to have access to WP functions like wp_redirect.

function myRedirector(){
    if ($_GET['somethingIcareAbout'] == "exists"){
        $url = "https://somewhere.com";
        wp_redirect( $url );

add_action( 'init', 'myRedirector');

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.