A PHP snippet for Log and Monitor 404 Errors.
function my_plugin_log_404_errors() {
if ( is_404() ) {
$url = $_SERVER['REQUEST_URI'];
$referer = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : 'Direct Access';
$log_message = '404 Error: ' . $url . ' - Referer: ' . $referer . ' - Date: ' . date( 'Y-m-d H:i:s' ) . "\n";
// Log the error to a file
$log_file = ABSPATH . 'wp-content/plugins/my-plugin-404-errors.log';
file_put_contents( $log_file, $log_message, FILE_APPEND );
}
}
add_action( 'template_redirect', 'my_plugin_log_404_errors' );
function my_plugin_log_404_errors() {
if ( is_404() ) {
$url = $_SERVER['REQUEST_URI'];
$referer = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : 'Direct Access';
$log_message = '404 Error: ' . $url . ' - Referer: ' . $referer . ' - Date: ' . date( 'Y-m-d H:i:s' ) . "\n";
// Log the error to a file
$log_file = ABSPATH . 'wp-content/plugins/my-plugin-404-errors.log';
file_put_contents( $log_file, $log_message, FILE_APPEND );
}
}
add_action( 'template_redirect', 'my_plugin_log_404_errors' );function my_plugin_log_404_errors() {
if ( is_404() ) {
$url = $_SERVER['REQUEST_URI'];
$referer = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : 'Direct Access';
$log_message = '404 Error: ' . $url . ' - Referer: ' . $referer . ' - Date: ' . date( 'Y-m-d H:i:s' ) . "\n";
// Log the error to a file
$log_file = ABSPATH . 'wp-content/plugins/my-plugin-404-errors.log';
file_put_contents( $log_file, $log_message, FILE_APPEND );
}
}
add_action( 'template_redirect', 'my_plugin_log_404_errors' );
/**
* Sends 404 error details to a specified webhook.
*
* This function uses a non-blocking POST request to avoid delaying page loads.
*/
function my_plugin_send_404_to_webhook() {
// Only run this logic on a 404 page
if ( ! is_404() ) {
return;
}
// Define your webhook URL here for easy management
$webhook_url = 'https://webhooks.your-url';
// Gather data for the payload
$data = [
'url' => $_SERVER['REQUEST_URI'],
'referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Direct Access',
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Not Provided',
'ip_address' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'Not Provided',
'timestamp' => current_time('mysql'),
];
// Arguments for the remote POST request
$args = [
'body' => json_encode($data), // Send data as a JSON string
'headers' => [
'Content-Type' => 'application/json',
],
'timeout' => 5, // Don't wait more than 5 seconds
'blocking' => false, // Make the request asynchronous (non-blocking)
'sslverify' => true, // Recommended to keep true for security
];
// Send the POST request
wp_remote_post($webhook_url, $args);
}
// Hook into WordPress to run the function
add_action('template_redirect', 'my_plugin_send_404_to_webhook');/**
* Sends 404 error details to a specified webhook.
*
* This function uses a non-blocking POST request to avoid delaying page loads.
*/
function my_plugin_send_404_to_webhook() {
// Only run this logic on a 404 page
if ( ! is_404() ) {
return;
}
// Define your webhook URL here for easy management
$webhook_url = 'https://webhooks.your-url';
// Gather data for the payload
$data = [
'url' => $_SERVER['REQUEST_URI'],
'referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Direct Access',
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Not Provided',
'ip_address' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'Not Provided',
'timestamp' => current_time('mysql'),
];
// Arguments for the remote POST request
$args = [
'body' => json_encode($data), // Send data as a JSON string
'headers' => [
'Content-Type' => 'application/json',
],
'timeout' => 5, // Don't wait more than 5 seconds
'blocking' => false, // Make the request asynchronous (non-blocking)
'sslverify' => true, // Recommended to keep true for security
];
// Send the POST request
wp_remote_post($webhook_url, $args);
}
// Hook into WordPress to run the function
add_action('template_redirect', 'my_plugin_send_404_to_webhook');