Entries Left Shortcode

Instructions

Read the Walkthrough

Code

Filename: display-number-of-entries-left.php

<?php
/**
 * Gravity Forms // Entries Left Shortcode
 * https://gravitywiz.com/shortcode-display-number-of-entries-left/
 *
 * Instruction Video: https://www.loom.com/share/b6c46aebf0ff483496faf9994e36083e
 *
 * Instructions: 
 * 
 * 1. Install the snippet.
 *    https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 */
add_filter( 'gform_shortcode_entries_left', 'gwiz_entries_left_shortcode', 10, 2 );
function gwiz_entries_left_shortcode( $output, $atts ) {
	// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
	extract( shortcode_atts( array(
		'id'     => false,
		'format' => false, // should be 'comma', 'decimal'
	), $atts ) );

	if ( ! $id ) {
		return '';
	}

	$form = GFAPI::get_form( $id );
	if ( ! rgar( $form, 'limitEntries' ) || ! rgar( $form, 'limitEntriesCount' ) ) {
		return '';
	}

	$entry_count = GFAPI::count_entries( $form['id'], array(
		'status' => 'active',
	) );

	$entries_left = rgar( $form, 'limitEntriesCount' ) - $entry_count;
	$output       = $entries_left;

	if ( $format ) {
		$format = $format == 'decimal' ? '.' : ',';
		$output = number_format( $entries_left, 0, false, $format );
	}

	return $entries_left > 0 ? $output : 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.