Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Last active March 31, 2025 06:48
Show Gist options
  • Save xlplugins/cab87f612426c73bcf4508110f84d300 to your computer and use it in GitHub Desktop.
Save xlplugins/cab87f612426c73bcf4508110f84d300 to your computer and use it in GitHub Desktop.
debug_log_plugin_activation.php
function debug_log_plugin_activation($plugin) {
// Ensure plugin is not empty
if (empty($plugin)) {
return; // Exit if no plugin name is provided
}
// Get current user information
$current_user = wp_get_current_user();
$user_id = isset($current_user->ID) ? $current_user->ID : 'N/A';
$user_name = isset($current_user->user_login) ? $current_user->user_login : 'Guest';
$user_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'Unknown IP';
$time = current_time('mysql');
// Validate and ensure the IP address and other values are not empty
if (empty($user_ip) || empty($user_name) || empty($time)) {
return; // If any important data is missing, do not log
}
// Get the backtrace summary for debugging purposes
$backtrace = wp_debug_backtrace_summary();
// Create the log entry message
$log_entry = sprintf(
"User: %s | User ID: %s | IP: %s | Time: %s | Plugin: %s | Backtrace: %s",
$user_name,
$user_id,
$user_ip,
$time,
$plugin,
$backtrace
);
// Get the WooCommerce logger instance
$logger = wc_get_logger();
// Log the entry under a custom channel, 'plugin-activation'
$logger->info($log_entry, array('source' => 'plugin-activation'));
}
// Hook into plugin activation
add_action('activated_plugin', 'debug_log_plugin_activation');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment