Skip to content

Instantly share code, notes, and snippets.

@yellowstonebum
Last active December 19, 2018 17:38
Show Gist options
  • Save yellowstonebum/4b76faed4c29e65fb01c136b2014c340 to your computer and use it in GitHub Desktop.
Save yellowstonebum/4b76faed4c29e65fb01c136b2014c340 to your computer and use it in GitHub Desktop.
Gravity Form - Max Submissions
<?php
// created by smtveter@gmail.com
// for Gravity Forms
// add to functions or to snippet plugin
// NEEDS - On line 15, add max allowed for what you want the redirect to do
// CUSTOMIZING - For certain forms, change '{form_id}' or add a conditional statement
add_filter( 'gform_pre_render_{form_id}', 'gf_form_count_{form_id}' );
function gf_form_count_{form_id}($form){
// Change per form
$user_id = get_current_user_id();
$meta_key = 'count_submissions';
$get_meta = get_user_meta($user_id, $meta_key, true);
// Max number of submissions
if ($get_meta => 3){
// Redirect.... or do something... might not work properly
protected $redirect_url = 'https://www.google.com';
}
return;
}
add_action( 'gform_after_submission_{form_id}', 'after_submit_{form_id}', 10, 2 );
function after_submit_{form_id}( $entry, $form, $field ) {
//Fetches User ID
$user_id = get_current_user_id();
//Meta Key, Identified further bellow
$meta_key = 'count_submissions';
//gets meta key for the current user
$get_meta = get_user_meta($user_id, $meta_key, true);
//Changes the quantitiy of the meta
$qty = $get_meta + 1;
//updates that qunatity in your database
update_user_meta( $user_id, $meta_key, $qty );
return;
}
// Creating custom user meta
function custom_user_profile_fields($profileuser) {
?>
<!--START Submission Count-->
<table class="form-table">
<tr>
<th>
<label for="count_submissions"><?php _e('Number of Form Sumbissions'); ?></label>
</th>
<td>
<input type="number" name="count_submissions" id="count_submissions" value="<?php echo esc_attr( get_the_author_meta( 'count_submissions', $profileuser->ID ) ); ?>" class="regular-text" />
<br><span class="description"><?php _e('Number of form submissions', 'text-domain'); ?></span>
</td>
</tr>
</table>
<!--END Submission Count-->
<?php
add_action('show_user_profile', 'custom_user_profile_fields', 10, 1);
add_action('edit_user_profile', 'custom_user_profile_fields', 10, 1);
add_action('personal_options_update', 'update_extra_profile_fields');
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) )
//Make sure this Meta matches the Input Fields above
update_user_meta($user_id, 'count_submissions', $_POST['count_submissions']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment