Gravity Forms: Managing Quotas Per Context (Event, Page, CPT…)

In a previous post https://wpster.com/gravity-forms-quotas/, I explained how to manage quotas in Gravity Forms and display a custom message and fallback form once the limit is reached. That approach works perfectly, as long as each form has its own independent quota.

However, it breaks down when the same form is reused across multiple pages or Custom Post Types (CPTs).

If you use a single registration form for multiple events, for example, Gravity Forms will count all entries globally. That means quotas won’t be applied per event, but across all events combined.

To solve this, we need to introduce a context value (such as the event name, page title, or page slug) so entries can be counted separately for each context.

In this example, we’ll use the page slug as the context value.

Step 1: Add a Hidden Context Field

Create a hidden field in your form.

In the field settings:

  • Go to Advanced
  • Set the Default Value to:
{embed_post:post_name}

This dynamically stores the current page slug when the form is displayed.

Step 2: Add the Shortcode Logic

Add the following code to your custom plugin (or MU-plugin):

<?php
add_shortcode('gf_quota_switch', function ($atts) {

	$atts = shortcode_atts([
		'form_id'        => 1,
		'limit'          => 10,
		'context_field'  => 41,
		'fallback_form'  => 3,
		'message'        => 'The quota has been reached.',
	], $atts);

	if (!class_exists('GFAPI')) {
		return '';
	}

	$form_id       = (int) $atts['form_id'];
	$fallback_form = (int) $atts['fallback_form'];
	$limit         = (int) $atts['limit'];
	$context_field = (string) (int) $atts['context_field'];

	// Contexte fiable: post affiché (embed réel)
	$post_id = 0;
	if (!empty($GLOBALS['post']) && !empty($GLOBALS['post']->ID)) {
		$post_id = (int) $GLOBALS['post']->ID;
	}
	if ($post_id <= 0 && function_exists('get_queried_object_id')) {
		$post_id = (int) get_queried_object_id();
	}

	// On utilise le slug (post_name) comme valeur de contexte
	$context_value = ($post_id > 0) ? (string) get_post_field('post_name', $post_id) : '';

	$search_criteria = [
		'status' => 'active',
	];

	if ($context_value !== '') {
		$search_criteria['field_filters'] = [
			'mode' => 'all',
			[
				'key'      => $context_field,
				'operator' => 'is',
				'value'    => $context_value,
			],
		];
	}

	$count = (int) GFAPI::count_entries($form_id, $search_criteria);

	if ($count < $limit) {
		return do_shortcode('[gravityform id="' . $form_id . '" title="false" ajax="true"]');
	}

	return '
		<div class="gf-quota-message">
			<p>' . esc_html($atts['message']) . '</p>
			' . do_shortcode('[gravityform id="' . $fallback_form . '" title="false" ajax="true"]') . '
		</div>
	';
});

?>

Usage

Insert the shortcode in your page like this:

[gf_quota_switch form_id="4" limit="10" context_field="17" fallback_form="3" message="Quota reached"]

Parameters

  • form_id
    The main form to display.
  • limit
    Maximum number of entries allowed for this specific context.
  • context_field
    The ID of the hidden field storing the page slug.
  • fallback_form
    The form displayed once the quota is reached.
  • message
    Message displayed when the quota is exceeded.

How It Works

  • The hidden field stores the page slug.
  • Entries are counted only when both:
    • The form ID matches
    • The context field value matches the current page slug
  • If the limit is reached:
    • A message is displayed
    • The fallback form replaces the original one
  • If not:
    • The original form is displayed

Submit a Comment

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