Tracking down redirects in Wordpress can be a nightmare

I wrote this plugin to trace all the redirects and add them to a response header if you have WP_DEBUG enabled

make sure you don’t this use this in a production env

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

<?php
/**
 * Plugin Name: RedirectTracer
 * Description: Adds debugging output to the HTTP header response to find out which file/function triggered a redirect.
 * TURN ON WP_DEBUG and Inspect the HTTP response in your browsers developer console to see the debug information. OR check the debug log
 * ----------------------------------------------------------------------------
 */

class RedirectTracer
{

    /**
     * Returns the singleton instance.
     * @return RedirectTracer
     */
    public static function instance()
    {
        static $instance = null;

        if (null === $instance) {
            $instance = new RedirectTracer();
        }

        return $instance;
    }

    /**
     * Constructor.
     */
    protected function __construct()
    {
        if (WP_DEBUG) {
            add_filter(
                'wp_redirect',
                array( $this, 'redirect_headers' ),
                9999
            );
        }
    }

    /**
     * Adds some redirect headers with debugging information to the response.
     */
    public function redirect_headers($location)
    {
        if ($location) {
            $trace = $this->get_trace();

            error_log('REDIRECT Backtrace:');
            error_log(print_r($trace, true));

            if (! headers_sent()) {
                foreach ($trace as $ind => $line) {
                    header("WPDev-Redirect-Trace-$ind: $line", false);
                }
            } else {
                echo "\n";
                foreach ($trace as $ind => $line) {
                    echo "<!-- wdpu-redirect-trace-$ind: $line -->\n";
                }
            }
        }

        return $location;
    }

    /**
     * Generates an array of stack-trace information. Each array item is a
     * simple string that can be directly output.
     * @return array Trace information
     */
    public function get_trace()
    {
        $result = array();

        $trace = debug_backtrace();
        $trace_count = count($trace);
        $_num = 0;
        $start_at = 0;

        // Skip the first 2 trace lines (filter call inside wp_redirect)
        if ($trace_count > 2) {
            $start_at = 2;
        }

        for ($i = $start_at; $i < $trace_count; $i += 1) {
            $trace_info = $trace[$i];
            $line_info = $trace_info;
            $j = $i;

            while (empty($line_info['line']) && $j < $trace_count) {
                $line_info = $trace[$j];
                $j += 1;
            }

            $_file = empty($line_info['file']) ? '' : $line_info['file'];
            $_line = empty($line_info['line']) ? '' : $line_info['line'];
            $_args = empty($trace_info['args']) ? array() : $trace_info['args'];
            $_class = empty($trace_info['class']) ? '' : $trace_info['class'];
            $_type = empty($trace_info['type']) ? '' : $trace_info['type'];
            $_function = empty($trace_info['function']) ? '' : $trace_info['function'];

            $_num += 1;
            $_arg_string = '';
            $_args_arr = array();

            if ($i > 0 && is_array($_args) && count($_args)) {
                foreach ($_args as $arg) {
                    if (is_scalar($arg)) {
                        if (is_bool($arg)) {
                            $_args_arr[] = ( $arg ? 'true' : 'false' );
                        } elseif (is_string($arg)) {
                            $_args_arr[] = '"' . $arg . '"';
                        } else {
                            $_args_arr[] = $arg;
                        }
                    } elseif (is_array($arg)) {
                        $_args_arr[] = '[Array]';
                    } elseif (is_object($arg)) {
                        $_args_arr[] = '[' . get_class($arg) . ']';
                    } elseif (is_null($arg)) {
                        $_args_arr[] = 'NULL';
                    } else {
                        $_args_arr[] = '[?]';
                    }
                }

                $_arg_string = implode(',', $_args_arr);
            }

            if (strlen($_file) > 80) {
                $_file = '...' . substr($_file, -77);
            } else {
                $_file = str_pad($_file, 80, ' ', STR_PAD_RIGHT);
            }

            $result_item = sprintf(
                '%s:%s %s(%s)',
                $_file,
                str_pad($_line, 5, ' ', STR_PAD_LEFT),
                $_class . $_type . $_function,
                $_arg_string
            );

            $_num_str = str_pad($_num, 2, '0', STR_PAD_LEFT);
            $result[$_num_str] = $result_item;
        }

        return $result;
    }
}
RedirectTracer::instance();

so you can run something like

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
❯ curl -I wordpress.docker.localhost/project
*   Trying 127.0.0.1:80...
* Connected to wordpress.docker.localhost (127.0.0.1) port 80 (#0)
> HEAD /project HTTP/1.1
> Host: wordpress.docker.localhost
> User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
HTTP/1.1 301 Moved Permanently
< Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: no-store, no-cache, must-revalidate
< Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8
< Date: Fri, 28 May 2021 23:47:40 GMT
Date: Fri, 28 May 2021 23:47:40 GMT
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Location: http://wordpress.docker.localhost/project/
Location: http://wordpress.docker.localhost/project/
< Pragma: no-cache
Pragma: no-cache
< Server: nginx/1.16.1
Server: nginx/1.16.1
< Set-Cookie: PHPSESSID=t9pbq86pf2ulquc1snqnln1g29; path=/
Set-Cookie: PHPSESSID=t9pbq86pf2ulquc1snqnln1g29; path=/
< Wpdev-Redirect-Trace-01: /app/wp-includes/plugin.php           : 212 WP_Hook->apply_filters("http://wordpress.docker.localhost/project/",[Array])
Wpdev-Redirect-Trace-01: /app/wp-includes/plugin.php             : 212 WP_Hook->apply_filters("http://wordpress.docker.localhost/project/",[Array])
< Wpdev-Redirect-Trace-02: /app/wp-includes/pluggable.php        : 1290 apply_filters("wp_redirect","http://wordpress.docker.localhost/project/",301)
Wpdev-Redirect-Trace-02: /app/wp-includes/pluggable.php          : 1290 apply_filters("wp_redirect","http://wordpress.docker.localhost/project/",301)
< Wpdev-Redirect-Trace-03: /app/wp-includes/canonical.php        : 799 wp_redirect("http://wordpress.docker.localhost/project/",301)
Wpdev-Redirect-Trace-03: /app/wp-includes/canonical.php          : 799 wp_redirect("http://wordpress.docker.localhost/project/",301)
< Wpdev-Redirect-Trace-04: /app/wp-includes/class-wp-hook.php    : 292 redirect_canonical("http://wordpress.docker.localhost/project")
Wpdev-Redirect-Trace-04: /app/wp-includes/class-wp-hook.php      : 292 redirect_canonical("http://wordpress.docker.localhost/project")
< Wpdev-Redirect-Trace-05: /app/wp-includes/class-wp-hook.php    : 316 WP_Hook->apply_filters(NULL,[Array])
Wpdev-Redirect-Trace-05: /app/wp-includes/class-wp-hook.php      : 316 WP_Hook->apply_filters(NULL,[Array])
< Wpdev-Redirect-Trace-06: /app/wp-includes/plugin.php           : 484 WP_Hook->do_action([Array])
Wpdev-Redirect-Trace-06: /app/wp-includes/plugin.php             : 484 WP_Hook->do_action([Array])
< Wpdev-Redirect-Trace-07: /app/wp-includes/template-loader.php  : 13 do_action("template_redirect")
Wpdev-Redirect-Trace-07: /app/wp-includes/template-loader.php    : 13 do_action("template_redirect")
< Wpdev-Redirect-Trace-08: /app/wp-blog-header.php               : 19 require_once("/app/wp-includes/template-loader.php")
Wpdev-Redirect-Trace-08: /app/wp-blog-header.php                 : 19 require_once("/app/wp-includes/template-loader.php")
< Wpdev-Redirect-Trace-09: /app/index.php                        : 17 require("/app/wp-blog-header.php")
Wpdev-Redirect-Trace-09: /app/index.php                          : 17 require("/app/wp-blog-header.php")
< X-Powered-By: PHP/7.3.28
X-Powered-By: PHP/7.3.28
< X-Redirect-By: WordPress
X-Redirect-By: WordPress

<
* Connection #0 to host wordpress.docker.localhost left intact