|
| 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