Skip to content

Commit c4185d6

Browse files
committed
Release 1.3.9
feat(debug): Added wp-redirect-log.php mu-plugin
1 parent f4c4dcd commit c4185d6

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## Release 1.3.9
3+
* feat(debug): Added wp-redirect-log.php mu-plugin
4+
5+
26
## Relase 1.3.8
37
* feat(debug): Added php-memory.php
48

README-snippets.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
| [PHP Memory Info](./debug/php-memory.php) | mu-plugin | :white_check_mark: | Adds a Tools -> PHP Memory page to display PHP ini and WordPress memory settings and current usage. |
5050
| [show-browser-cookies.php](./debug/show-browser-cookies.php) | snippet | :white_check_mark: | * Description Shows your browsers cookies |
5151
| [Application Logs](./debug/show-logs-admin.php) | Plugin | :white_check_mark: | Displays top 10 lines from a log file in the WordPress admin section. |
52+
| [WP Redirect Log](./debug/wp-redirect-log.php) | mu-plugin | :white_check_mark: | Logs all wp_redirect and wp_safe_redirect calls to a log file in the uploads directory. |
5253

5354
## [development](development)
5455

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ The status field is used to indicate the current status of the snippet. This is
6363
| [PHP Memory Info](./debug/php-memory.php) | mu-plugin | :white_check_mark: | Adds a Tools -> PHP Memory page to display PHP ini and WordPress memory settings and current usage. |
6464
| [show-browser-cookies.php](./debug/show-browser-cookies.php) | snippet | :white_check_mark: | * Description Shows your browsers cookies |
6565
| [Application Logs](./debug/show-logs-admin.php) | Plugin | :white_check_mark: | Displays top 10 lines from a log file in the WordPress admin section. |
66+
| [WP Redirect Log](./debug/wp-redirect-log.php) | mu-plugin | :white_check_mark: | Logs all wp_redirect and wp_safe_redirect calls to a log file in the uploads directory. |
6667

6768
## [development](development)
6869

@@ -137,6 +138,10 @@ The status field is used to indicate the current status of the snippet. This is
137138
| [Failover Status Monitor](./wp-failover/wp-failover.php) | Plugin | :white_check_mark: | Monitors failover status and provides notifications. |
138139

139140
# Changelog
141+
## Release 1.3.9
142+
* feat(debug): Added wp-redirect-log.php mu-plugin
143+
144+
140145
## Relase 1.3.8
141146
* feat(debug): Added php-memory.php
142147

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.3.8
1+
1.3.9

debug/wp-redirect-log.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Plugin Name: WP Redirect Log
4+
* Description: Logs all wp_redirect and wp_safe_redirect calls to a log file in the uploads directory.
5+
* Version: 1.0
6+
* Author: ManagingWP
7+
* Author URI: https://github.com/managingwp/wordpress-code-snippets
8+
* Type: mu-plugin
9+
* Status: Complete
10+
*/
11+
12+
if ( ! defined( 'ABSPATH' ) ) {
13+
exit;
14+
}
15+
16+
// Hook into WP redirects
17+
add_filter( 'wp_redirect', 'wprl_log_redirect', 10, 2 );
18+
add_filter( 'wp_safe_redirect', 'wprl_log_redirect', 10, 2 );
19+
20+
// Also catch raw Location headers
21+
add_filter( 'wp_headers', 'wprl_log_raw_header' );
22+
23+
/**
24+
* Get full path to our log file in uploads/
25+
*
26+
* @return string
27+
*/
28+
function wprl_get_log_file() {
29+
$uploads = wp_upload_dir();
30+
$basedir = trailingslashit( $uploads['basedir'] );
31+
$file = $basedir . 'wp-redirects.log';
32+
33+
if ( ! file_exists( $file ) ) {
34+
@touch( $file );
35+
@chmod( $file, 0664 );
36+
}
37+
38+
return $file;
39+
}
40+
41+
/**
42+
* Append a line to our log file
43+
*
44+
* @param string $line
45+
* @return void
46+
*/
47+
function wprl_write_log( $line ) {
48+
$file = wprl_get_log_file();
49+
file_put_contents( $file, $line . PHP_EOL, FILE_APPEND | LOCK_EX );
50+
}
51+
52+
/**
53+
* Log wp_redirect / wp_safe_redirect usage
54+
*
55+
* @param string $location
56+
* @param int $status
57+
* @return string
58+
*/
59+
function wprl_log_redirect( $location, $status ) {
60+
// short backtrace to find caller
61+
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 5 );
62+
$caller = isset( $trace[2] ) ? $trace[2] : $trace[1];
63+
$caller_info = '';
64+
if ( ! empty( $caller['file'] ) ) {
65+
$caller_info = wp_basename( $caller['file'] ) . ':' . ( $caller['line'] ?? 0 );
66+
}
67+
68+
$msg = sprintf(
69+
"[WP-REDIRECT] %s → %s (status %d) called by %s",
70+
$_SERVER['REQUEST_URI'] ?? 'unknown',
71+
$location,
72+
$status,
73+
$caller_info
74+
);
75+
wprl_write_log( $msg );
76+
77+
return $location;
78+
}
79+
80+
/**
81+
* Log any raw Location headers before send
82+
*
83+
* @param array $headers
84+
* @return array
85+
*/
86+
function wprl_log_raw_header( $headers ) {
87+
if ( ! empty( $headers['Location'] ) ) {
88+
$msg = sprintf(
89+
"[WP-HEADER] sending Location: %s (on %s)",
90+
$headers['Location'],
91+
$_SERVER['REQUEST_URI'] ?? 'unknown'
92+
);
93+
wprl_write_log( $msg );
94+
}
95+
return $headers;
96+
}

0 commit comments

Comments
 (0)