/**
  * When WordPress sees a url like http://foobar.com/nggallery/page/2/, it thinks that it is an
  * invalid url. Therefore, we modify the request uri before WordPress parses the request, and then
  * restore the request uri afterwards
  */
 function restore_request_uri()
 {
     if (isset($_SERVER['ORIG_REQUEST_URI'])) {
         $request_uri = $_SERVER['ORIG_REQUEST_URI'];
         $_SERVER['UNENCODED_URL'] = $_SERVER['HTTP_X_ORIGINAL_URL'] = $_SERVER['REQUEST_URI'] = $request_uri;
     } else {
         wp_old_slug_redirect();
         redirect_canonical();
     }
 }
 /**
  * @ticket 35031
  */
 public function test_old_slug_doesnt_redirect_when_reused()
 {
     $old_permalink = user_trailingslashit(get_permalink($this->post_id));
     wp_update_post(array('ID' => $this->post_id, 'post_name' => 'bar-baz'));
     $new_post_id = self::factory()->post->create(array('post_title' => 'Foo Bar', 'post_name' => 'foo-bar'));
     $permalink = user_trailingslashit(get_permalink($new_post_id));
     $this->assertEquals($old_permalink, $permalink);
     $this->go_to($old_permalink);
     wp_old_slug_redirect();
     $this->assertNull($this->old_slug_redirect_url);
 }
 /**
  * When WordPress sees a url like http://foobar.com/nggallery/page/2/, it thinks that it is an
  * invalid url. Therefore, we modify the request uri before WordPress parses the request, and then
  * restore the request uri afterwards
  */
 function restore_request_uri()
 {
     if (isset($_SERVER['ORIG_REQUEST_URI'])) {
         $request_uri = $_SERVER['ORIG_REQUEST_URI'];
         $_SERVER['UNENCODED_URL'] = $_SERVER['HTTP_X_ORIGINAL_URL'] = $_SERVER['REQUEST_URI'] = $request_uri;
         if (isset($_SERVER['ORIG_PATH_INFO'])) {
             $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
         }
     } else {
         if (self::$_use_old_slugs) {
             wp_old_slug_redirect();
         }
         if (self::$_use_canonical_redirect) {
             redirect_canonical();
         }
     }
 }
 public function test_old_slug_redirect_paged()
 {
     wp_update_post(array('ID' => $this->post_id, 'post_content' => 'Test<!--nextpage-->Test'));
     $old_permalink = user_trailingslashit(trailingslashit(get_permalink($this->post_id)) . 'page/2');
     wp_update_post(array('ID' => $this->post_id, 'post_name' => 'bar-baz'));
     $permalink = user_trailingslashit(trailingslashit(get_permalink($this->post_id)) . 'page/2');
     $this->go_to($old_permalink);
     wp_old_slug_redirect();
     $this->assertEquals($permalink, $this->old_slug_redirect_url);
 }
/**
 * try to redirect by old slug in case the current result is 404
 *
 */
function try_redirect()
{
    global $wp_query;
    if ($wp_query->is_404 == true) {
        \wp_old_slug_redirect();
    }
}