Created
February 6, 2018 04:18
-
-
Save jaredatch/1d2a801c3008b6beacd918dc1baaae47 to your computer and use it in GitHub Desktop.
WPForms limit form entries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check entry limit, if past limit display a message. | |
* | |
* @param array $form_data | |
* @param object $form | |
*/ | |
function wpf_form_check_entry_limit( $form_data, $form ) { | |
global $wpf_form_open; | |
$wpf_form_open = true; | |
$wpf_form_id = 2161; | |
$wpf_form_limit = 3; | |
if ( ! empty( $_POST['wpforms']['id'] ) ) { | |
return; | |
} | |
if ( $wpf_form_id == $form_data['id'] ) { | |
$entries = wpforms()->entry->get_entries( | |
array( | |
'form_id' => $wpf_form_id, | |
), | |
true | |
); | |
if ( $entries >= $wpf_form_limit ) { | |
$wpf_form_open = false; | |
echo 'This form is now closed.'; | |
} | |
} | |
} | |
add_action( 'wpforms_frontend_output_before', 'wpf_form_check_entry_limit', 10, 2 ); | |
/** | |
* Don't show the form if it is past it's entry limit | |
* | |
* @param bool $show | |
* @param array $form_data | |
* @return bool | |
*/ | |
function wpf_form_check_show_form( $show, $form_data ) { | |
$wpf_form_id = 2161; | |
if ( $wpf_form_id == $form_data['id'] ) { | |
global $wpf_form_open; | |
return $wpf_form_open; | |
} | |
return $show; | |
} | |
add_filter( 'wpforms_frontend_load', 'wpf_form_check_show_form', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment