Draft Groups by Page

Create draft groups where only drafts saved from the current URL will be displayed. For example, if you’re using the same form on multiple pages, you can use this snippet to display drafts saved from the current page only.

Code

Filename: gpasc-draft-groups-by-page.php

<?php
/**
 * Gravity Perks // Advanced Save & Continue // Draft Groups by Page
 * https://gravitywiz.com/documentation/gravity-forms-advanced-save-continue/
 *
 * Create draft groups where only drafts saved from the current URL will be displayed. For example, if you're using the
 * same form on multiple pages, you can use this snippet to display drafts saved from the current page only.
 */
add_filter( 'gpasc_form_resume_tokens', function( $tokens, $form_id ) {

	// Set ignore parameters to false if you would like parameters to be included in identifying unique pages.
	$ignore_parameters = true;

	if ( wp_doing_ajax() ) {
		return $tokens;
	}

	$target_value = $_SERVER['REQUEST_URI'];
	if ( $ignore_parameters ) {
		$target_value = strtok( $target_value, '?' );
	}

	foreach ( $tokens as $index => $token ) {
		// If token is specifically passed via parameter, don't interfere.
		$query_token = rgget( 'gf_token' );
		if ( $query_token && $query_token === $token['token'] ) {
			continue;
		}
		$token_path = $token['form_path'];
		if ( $ignore_parameters ) {
			$token_path = strtok( $token['form_path'], '?' );
		}
		if ( $token_path !== $target_value ) {
			unset( $tokens[ $index ] );
		}
	}

	return array_filter( $tokens );
}, 10, 2 );

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.