Skip to content

Instantly share code, notes, and snippets.

@chrisegg
Last active March 14, 2025 01:04
Show Gist options
  • Save chrisegg/33a55ee23c76ebc576b2840a8f15650b to your computer and use it in GitHub Desktop.
Save chrisegg/33a55ee23c76ebc576b2840a8f15650b to your computer and use it in GitHub Desktop.
This shortcode dynamically calculates and displays the remaining availability entries for a Gravity Forms form based on the number of form entries submitted and the entry limit settings.
<?php
/**
* Entries Remaining Shortcode
*
* This shortcode dynamically calculates and displays the remaining availabile entries
* for a Gravity Forms form based on the number of form entries submitted.
*
* Usage: Enable entry limits in form settings and set your limit number. Then add the shortcode [spots_available] to an
* HTML field in your form or to a WordPress post or page where a Gravity Form is embedded.
*
* Author: Chris Eggleston
* Version: 1.0
* Credit: This code is an improved version of an idea and code snippet provided by Rowan Schoon of visualpower.agency
*
*
*/
function check_spots_available($atts) {
$post_id = get_the_ID();
// Retrieve the content of the post
$post_content = get_post_field('post_content', $post_id);
error_log("Post Content: " . $post_content); // Log the post content for debugging
$form_id = null;
// Check for Gravity Forms shortcode in the post content
if (preg_match('/\[gravityform.*?id="?(\d+)"?.*?\]/i', $post_content, $matches)) {
$form_id = $matches[1];
error_log("Form ID found via shortcode: " . $form_id);
}
// Check for Gravity Forms block editor embedding in the post content
if (!$form_id && preg_match('/<!-- wp:gravityforms\/form {"formId":"(\d+)".*} \/-->/', $post_content, $matches)) {
$form_id = $matches[1];
error_log("Form ID found via block: " . $form_id);
}
// If no form ID is found, return an error message
if (!$form_id) {
return '<span style="color: red; display: block; padding: 10px; margin-top: 10px;">Error: Form not found in content.</span>';
}
// Retrieve the form settings using the Gravity Forms API
$form_settings = GFAPI::get_form($form_id);
// Fetch the entry limit from the form settings
$limit = isset($form_settings['limitEntriesCount']) ? $form_settings['limitEntriesCount'] : 0;
error_log("Dynamically fetched Entry limit: " . $limit);
// If no entry limit is set, assume unlimited availability
if ($limit == 0) {
return "<span class='availability-label available' style='background-color: green; color: white; padding: 10px; margin-top: 15px; display: inline-block;'>Available</span>";
}
// Retrieve all active form entries count
$search_criteria = array();
$entry_count = GFAPI::count_entries($form_id, $search_criteria);
// Handle errors in fetching entries
if (is_wp_error($entry_count)) {
return '<span style="color: red; display: block; padding: 10px; margin-top: 10px;">Error: Failed to fetch form entries.</span>';
}
error_log("Entry count: " . $entry_count);
// Calculate the remaining spots
$remaining = max($limit - $entry_count, 0);
error_log("Remaining spots: " . $remaining);
// Determine the availability status and style based on the remaining spots
if ($remaining >= 5) {
$label = "$remaining spots available";
$status = 'available';
$color = 'green';
} elseif ($remaining > 0 && $remaining < 5) {
$label = "only $remaining spots left";
$status = 'almost-full';
$color = 'orange';
} else {
$label = "Full";
$status = 'full';
$color = 'red';
}
// Return the styled availability label
return "<span class='availability-label $status' style='background-color: $color; color: white; padding: 10px; margin-top: 15px; display: inline-block;'>$label</span>";
}
// Register the shortcode [spots_available] for use in WordPress content
add_shortcode('spots_available', 'check_spots_available');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment