Last active
February 22, 2024 09:36
-
-
Save wpmudev-sls/59f0bc3e6f72ad61bbdd3e8a361a9b97 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Limit submission by user except admins
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 | |
/** | |
* Plugin Name: [Forminator Pro] Limit submission by user | |
* Description: Limit submission by user except admin | |
* Author: Prashant @ WPMUDEV | |
* Task: SLS-5808 | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ) { | |
if ( ! is_user_logged_in() ) { | |
return $submit_errors; | |
} | |
if ( current_user_can( 'manage_options' ) ) { | |
return $submit_errors; | |
} | |
// Add your form IDs here. | |
$form_ids = array( 1609 ); | |
// Change this to the message that you want to show. | |
$message = 'You cannot submit more than 3 times.'; | |
if ( in_array( intval( $form_id ), $form_ids, true ) ) { | |
$user_id = get_current_user_id(); | |
if ( ! empty( $user_id ) ) { | |
$last_entry = wpmudev_get_last_few_entries( $form_id, $user_id ); | |
if ( $last_entry ) { | |
$GLOBALS['form_limit_error'] = false; | |
} else { | |
$submit_errors[]['submit'] = $message; | |
$GLOBALS['form_limit_error'] = true; | |
} | |
} | |
} | |
return $submit_errors; | |
}, 15, 3 ); | |
function wpmudev_get_last_few_entries( $form_id, $user_id ) { | |
global $wpdb; | |
$table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY_META ); | |
$entry_table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY ); | |
$sql = "SELECT m.entry_id FROM {$table_name} m LEFT JOIN {$entry_table_name} e ON(e.entry_id = m.entry_id) WHERE e.form_id = %d AND m.meta_key = %s AND m.meta_value = %s order by m.meta_id"; | |
$entrys = $wpdb->get_results( $wpdb->prepare( $sql, $form_id, 'hidden-1', $user_id ) ); | |
if ( $wpdb->last_error ) { | |
return false; | |
} else { | |
if ( count( $entrys ) >= 3 ) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
return false; | |
} | |
add_filter( 'forminator_custom_form_invalid_form_message', 'wpmudev_invalid_form_error_response_submit_msg', 10, 2 ); | |
function wpmudev_invalid_form_error_response_submit_msg( $invalid_form_message, $form_id ) { | |
if ( $form_id != 1609 ) { // Please change the form ID. | |
return $invalid_form_message; | |
} | |
if ( $GLOBALS['form_limit_error'] ) { | |
$invalid_form_message = __( 'You cannot submit more than 3 times.', 'forminator' ); | |
} | |
return $invalid_form_message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment