[英]Gravity Forms - Prevent Multiple Form Entries Same User ID
我們正在使用重力表單,需要防止用戶在提交后再次訪問表單。 這是我們在“application.php”模板中使用的代碼:
<?php
if (get_current_user_id()) {
//lookup current application:
$query = "SELECT * FROM dg_application where user_id = " . get_current_user_id();
$dg_application = $wpdb->get_row( $query, ARRAY_A );
if ($dg_application["user_id"] && $dg_application["status"] == "Completed") {
// custom field
echo get_post_meta($post->ID, "form_complete_message", true);
} else {
//let Gravity Forms/WP do thier jobs
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'template-parts/content', 'page' );
// End of the loop.
endwhile;
}
} else {
echo "Sorry. You must be logged in to view this form.";
}
?>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
但它不起作用,也沒有像它應該的那樣顯示消息。 數據庫名稱正確。 我們正在使用自定義數據庫。 任何想法為什么它可能不顯示?
我嘗試了很多方法。 不確定這是否更好,但對我來說效果很好。 如果當前用戶已經填寫了表單,此代碼將阻止他們訪問表單。
add_filter( 'gform_get_form_filter_3', 'custom_schedule', 10, 2 );
function custom_schedule( $form_string, $form ) {
$current_user = wp_get_current_user();
$search_criteria = array(
'status' => 'active',
'field_filters' => array( //which fields to search
array(
'key' => 'created_by', 'value' => $current_user->ID, //Current logged in user
)
)
);
$form_id = 3;
$entry = GFAPI::get_entries($form_id,$search_criteria);
if ( !empty($entry) ) {
$form_string = '<p>Sorry, you have reached the submission limit for this form.</p>';
}
return $form_string;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.