0

我们正在使用重力表单,需要防止用户在提交后再次访问表单。这是我们在“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' ); ?>

但它不起作用,也没有像应有的那样显示消息。数据库名称正确。我们正在使用自定义数据库。任何想法为什么它可能不会显示?

4

1 回答 1

0

我尝试了很多方法。不确定这是否是更好的方法,但它对我来说效果很好。如果当前用户已经填写了表单,此代码将阻止他们访问表单。

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;
}
于 2021-04-06T10:39:04.617 回答