Skip to content

Instantly share code, notes, and snippets.

@chrisegg
Last active February 5, 2025 23:49
Show Gist options
  • Save chrisegg/5f6efef2f175d79116e328bd9d28c4f0 to your computer and use it in GitHub Desktop.
Save chrisegg/5f6efef2f175d79116e328bd9d28c4f0 to your computer and use it in GitHub Desktop.
Change the entry date with a new meta box in the entry details page.
<?php
/**
* Plugin Name: Gravity Forms Change Entry Date
* Plugin URI: https://gravityranger.com/gravity-forms-changing-the-entry-date
* Description: Adds a meta box to Gravity Forms entry details page allowing admins to change the entry date.
* Version: 1.0
* Author: Chris Eggleston
* Author URI: https://gravityranger.com
* License: GPL-2.0+
*/
if (!defined('ABSPATH')) {
exit; // Prevent direct access
}
// Add Meta Box to Entry Details Page
add_filter('gform_entry_detail_meta_boxes', function ($meta_boxes, $entry) {
$meta_boxes['update_entry_date'] = [
'title' => 'Change Entry Date',
'callback' => 'gf_render_entry_date_meta_box',
'context' => 'normal',
];
return $meta_boxes;
}, 10, 2);
function gf_render_entry_date_meta_box($args) {
$entry_id = rgget('lid'); // Get entry ID from URL
if (!$entry_id) {
echo '<div class="notice notice-error"><p>Invalid entry ID.</p></div>';
return;
}
$entry = GFAPI::get_entry($entry_id); // Fetch entry
if (is_wp_error($entry) || empty($entry) || !isset($entry['date_created'])) {
echo '<div class="notice notice-error"><p>Invalid entry data.</p></div>';
return;
}
$current_date = $entry['date_created'];
$current_date_only = date('Y-m-d', strtotime($current_date));
$current_time_only = date('H:i:s', strtotime($current_date));
?>
<div style="padding: 10px;">
<?php if (isset($_GET['update_success']) && $_GET['update_success'] == '1') : ?>
<div class="notice notice-success">
<p>Entry date updated successfully.</p>
</div>
<?php endif; ?>
<form method="post" style="margin-top: 20px;">
<p><strong>Current Date:</strong> <?php echo esc_html($current_date); ?></p>
<p>
<label for="new_entry_date"><strong>New Date (YYYY-MM-DD):</strong></label>
<input type="text" id="new_entry_date" name="new_entry_date" value="<?php echo esc_attr($current_date_only); ?>" style="width: 100%; max-width: 300px;">
</p>
<?php wp_nonce_field('update_entry_date', 'update_entry_date_nonce'); ?>
<p>
<input type="submit" name="update_entry_date_submit" class="button button-primary" value="Update Date">
</p>
</form>
</div>
<?php
// Handle Form Submission
if (isset($_POST['update_entry_date_submit'])) {
gf_handle_entry_date_update($entry_id, $current_time_only);
}
}
function gf_handle_entry_date_update($entry_id, $current_time_only) {
// Verify Nonce
if (!isset($_POST['update_entry_date_nonce']) || !wp_verify_nonce($_POST['update_entry_date_nonce'], 'update_entry_date')) {
echo '<div class="notice notice-error"><p>Security check failed.</p></div>';
return;
}
$new_date_only = sanitize_text_field($_POST['new_entry_date']);
// Validate New Date Format
if (DateTime::createFromFormat('Y-m-d', $new_date_only) === false) {
echo '<div class="notice notice-error"><p>Invalid date format. Use YYYY-MM-DD.</p></div>';
return;
}
// Combine New Date with Original Time
$new_date = $new_date_only . ' ' . $current_time_only;
// Update Entry Date
$result = GFAPI::update_entry_property($entry_id, 'date_created', $new_date);
if (is_wp_error($result)) {
echo '<div class="notice notice-error"><p>Error updating entry date: ' . esc_html($result->get_error_message()) . '</p></div>';
} else {
// Redirect to avoid form resubmission
$redirect_url = add_query_arg(['update_success' => '1'], remove_query_arg(['update_success']));
wp_safe_redirect($redirect_url);
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment