Skip to content

Gravity Forms Tip: How to Access Log Files Directly from the Entry Details Page

Chris Eggleston 2 min read

The Problem

We troubleshoot and review a good amount of forms for Gravity Forms users, and I was recently annoyed that I could not quickly access the logs from the entry details page. Instead, you would need to go to Forms > Settings > Logs, which is a few too many steps when you’re in a hurry.

I thought the entry details page would be a smart place to have a button since the notes section on that page is often the first place a user would look for an indication of an issue.

How To Fix It

I initially wanted to add the button to the notes section, but decided it would be best to keep the button separate and instead added a new meta box to the page.

As you can see, this solution only displays a button for the core log files. With some code modification, you could add a button for any add-ons with enabled logging.

This is the code that makes this possible:

entry-details-view-log-button-metabox.phpGist by chriseggGitHub
<?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;
}

How To Implement This Code

Copy and paste the code above to your themes functions.php file or use a code snippet plugin like WPCodeBox.

You do not need to make any modifications, but logging will need to be enabled. If it is not, instead of a button, you will see this message: “Log file not found.”.

If you wanted to make this a separate plugin, you could do that as well.

  1. Create a new folder (directory), name it gr-log-button
  2. Create a new PHP file with that folder/directory, name it gf-log-button.php
  3. Copy and paste the plugin header below to the file.
  4. Copy and paste the code above to a file below the plugin header.
  5. Save your changes.
  6. Zip up the file.
  7. Upload the file via the WordPress plugin installer.
  8. Activate the plugin.
<?php
/*
Plugin Name: GravityRanger GF Log Button
Description: Adds a button to view Gravity Forms core log file within the entry details page.
Version: 1.0
Author: Chris Eggleston
Site: https://gravityranger-v2.local
*/

Need More Help?

If you still need help, use the chat or leave a comment below and we’ll get back to you.

Comments

Ask a question or leave a comment

Your email is never shown. For help with a specific form, share what you expected and what happened instead.