With the following code snippet you can easily use GREYD.Forms to create a form that only allows a limited number of form submissions and displays a corresponding info message. Please be aware of the comments in the snippet as the code needs to be adapted individually.
if ( ! defined( 'COUNTER_FORM_ID' ) ) {
/**
* Replace this with the ID of your form.
*/
define( 'COUNTER_FORM_ID', 1 );
}
if ( ! defined( 'MAX_FORM_COUNT' ) ) {
/**
* Define the maximum number of entries.
*/
define( 'MAX_FORM_COUNT', 100 );
}
/**
* Handle the form submission.
*
* @param int $post_id The ID of the form.
* @param array $form_data The form data.
*/
function handle_custom_counter_form( $post_id, $form_data ) {
// Check if the form is the one we want to handle.
if ( $post_id != COUNTER_FORM_ID ) return;
// Get the current counter value.
$counter = intval( get_option( 'my_custom_counter', 0 ) );
// The maximum number of entries has been reached.
if ( $counter >= MAX_FORM_COUNT ) {
do_action(
'formhandler_error',
// The error message.
__( 'The maximum number of entries has been reached.', 'my_textdomain' )
);
return;
}
$counter++;
update_option( 'my_custom_counter', $counter );
// debug message
// remove this line in production
do_action('formhandler_error', 'counter is now: ' . $counter);
}
add_action( 'formhandler_after_entry', 'handle_custom_counter_form', 5, 3);