In some cases, when building a plugin admin page for WordPress, we is faced with situation where a page redirection is needed due to some circumstances.

Like for example, if we are building an admin form that must have certain query attached in the url and redirect the user to other form if those query is not found.

http://example.com/wp-admin/admin.php?page=myplugin&instance=123&delta=123 -> show the form

http://example.com/wp-admin/admin.php?page=myplugin&instance = 123 -> redirect the form to parent page

Sure we can do some logic separation in the plugin code such as :

 

if (!isset($_GET['delta'])) {
   // serve the other form
}
else {
  // serve the form
}

 

But IMO it is way cleaner to do this via routing to different page rather than creating if .. then .. for every possible situation. Why? it is easier to maintain the code later on when you need to adjust the redirection rule if you have many forms with many different redirection logic, rather than searching all the files for the if.. then.. rules.

 

So just implement this class and invoke it at the earliest possible place (surely not hooking this into wp_footer...) :

<?php
/**
 * General Class for handling redirection in the
 * WordPress admin area
 *
 * The rules for doing redirection is performed in
 * the class redirectLogic method.
 *
 * This class depends on hook filter wp_redirect and
 * hook action auth_redirect.
 *
 * The logic is since admin.php will call the auth_redirect()
 * function before anything else (the earliest point that
 * possible to be hooked into) and in the function WordPress
 * will perform auth_redirect hook if everything is ok or
 * calls multiple wp_redirect() if page needs to be redirected.
 *
 * Then when auth_redirect performs, we piggyback the function
 * to add additional logic.
 *
 * @author jason.xie@victheme.com
 *
 */
class Redirect {

  public function __construct() {
    // Controling the redirection logic
    add_filter('wp_redirect', array($this, 'pageRedirect'), 10, 2);
    add_action('auth_redirect', array($this,'authRedirect'));
  }

  public function authRedirect() {

    $pages = array(
       'somePageSlug'
    );

    if (isset($_GET['page']) && in_array($_GET['page'], $pages)) {
      $location = $this->redirectLogic($_SERVER['REQUEST_URI']);

      if ($location != $_SERVER['REQUEST_URI']) {
        $redirect = true;
      }
    }

    if (isset($redirect)) {
      ob_start();
      wp_redirect($location);
      if (ob_get_length()) {
        ob_end_clean();
      }
      exit();
    }
  }


  public function pageRedirect($location, $status) {
    $pages = array(
       'somePageSlug'
    );

    if (isset($_GET['page']) && in_array($_GET['page'], $pages)) {
      $location = $this->redirectLogic($location);
    }

    return $location;
  }


  public function redirectLogic($location) {

   if (.....some rules) {
      // Example to change the "page" query
      $location = remove_query_arg('page');
      $location = add_query_arg(array('page' => 'coolpage'), $location);
  }

    return $location;
  }

}

 

How to invoke this? just call the class and it will be automatically hooked :

 

<?php

// Invoking the redirection
$redirect = new Redirect();