Skip to content

Instantly share code, notes, and snippets.

@chrisegg
Created October 16, 2023 23:38
Show Gist options
  • Save chrisegg/84878ff47b08e7316f1b6ae390b5e4e1 to your computer and use it in GitHub Desktop.
Save chrisegg/84878ff47b08e7316f1b6ae390b5e4e1 to your computer and use it in GitHub Desktop.
This snippet adds a button to the entry details page that allows you to easily view the Gravity Forms core log file when enabled.
<?php
//Copy everything below this line
/**
*
* Entry Details Log Button
*
* This snippet adds a button to the entry details page that allows you to easily view the Gravity Forms core log file when enabled.
*
* @Version: 1.0
* @Author: Chris Eggleston
* @license: GPL-2.0+
* @link: https://gravityranger.com/
*
*/
// Hook into the gform_entry_detail_meta_boxes filter
add_filter('gform_entry_detail_meta_boxes', 'add_custom_meta_box_to_entry_details', 10, 3);
function add_custom_meta_box_to_entry_details($meta_boxes, $entry, $form) {
// Add your custom meta box
$meta_boxes['custom_button_meta_box'] = array(
'title' => 'Core Log Access',
'callback' => 'render_custom_button_meta_box',
'context' => 'normal', // Change to 'side' to place it in the sidebar content area
'priority' => 'high',
);
return $meta_boxes;
}
function render_custom_button_meta_box($entry, $form) {
// Specify the path to the Gravity Forms log directory
$log_directory = WP_CONTENT_DIR . '/uploads/gravity_forms/logs/';
// Find the most recent log file in the directory
$latest_log_file = get_latest_log_file($log_directory);
// Check if a log file exists
if (!empty($latest_log_file) && file_exists($latest_log_file)) {
// Extract the file name from the path
$log_file_name = basename($latest_log_file);
// Generate the correct URL to the most recent log file
$log_file_url = esc_url(site_url('/wp-content/uploads/gravity_forms/logs/' . $log_file_name));
// Output a description
echo '<p>This button allows you to easily view the Gravity Forms core log file if they are enabled. If the are not enabled you can enable them in the <a href="/wp-admin/admin.php?page=gf_settings" target=_blank">Settings page</a>.</p>';
// Output your custom button HTML with the correct log file URL
echo '<a href="' . $log_file_url . '" class="button" target="_blank">View Log</a>';
} else {
// If no log files exist, display a message
echo 'Log file not found.';
}
}
function get_latest_log_file($directory) {
$log_files = glob($directory . 'gravityforms_*.txt');
if (!empty($log_files)) {
rsort($log_files);
return $log_files[0];
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment