# Log Every Redirect With Its Source

> Writes each redirect WordPress makes to the error log, including the target, status code and the file that triggered it, to track down redirect loops.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: debugging, redirects, logs
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dev-log-redirects/

## Code

```php
add_filter( 'wp_redirect', function ( $location, $status ) {
    $source = '';

    foreach ( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ) as $frame ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions
        if ( isset( $frame['file'] ) && false === strpos( $frame['file'], 'wp-includes' ) ) {
            $source = str_replace( ABSPATH, '', $frame['file'] ) . ':' . ( $frame['line'] ?? '?' );
            break;
        }
    }

    $from = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';

    error_log( sprintf( '[redirect] %d %s -> %s (from %s)', $status, $from, $location, $source ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions

    return $location;
}, 999, 2 );
```

## Notes

Meant for troubleshooting; turn it off afterwards as it can write many log lines. Enable WP_DEBUG_LOG to read the entries.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Log Every Redirect With Its Source" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
