Home > Blockchain >  Gravity Forms - How can I show a default message instead of a Gravity Form if the user has already s
Gravity Forms - How can I show a default message instead of a Gravity Form if the user has already s

Time:08-12

We need a function that makes a specific Gravity form not show if the user has already submitted it once. There should be a default message displayed in its place. This form is already only available to specific logged in users so the extra code for that is not necessary.

This code is part of the code on this page: https://aaronjerad.com/blog/check-if-user-submitted-gravity-form/

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 = 1;
    $entry = GFAPI::get_entries($form_id,$search_criteria);

    if ( !empty($entry) ) {
        $form_string = '<p>You have already submitted this form. If you would like to make changes to your profile please contact us.</p>';
    }   
    return $form_string;
}

It should work and almost does but I am having one problem: The Form ID is not hiding the correct form.

I only have two gravity forms on my site. Their IDs are 1 and 3 and I want to hide the form with the ID of 1 if the user already submitted it once. If I set it to 1, it hides the form with the ID of 3. If I set it to 3 it does nothing. I tried with quotation marks (i.e. "1" and "3") and it made no difference. Strangely if I set it to 0 it hides form 3 as well but no other numbers I have tried affect either form.

I have checked in Gravity forms and the ids are 1 and 3 to the respective forms. Also, on the pages that each form is on, the shortcode for Gravity forms is using the correct IDs for what I want to accomplish. I also tried swapping the forms on their respective pages in case it was something in the code of the page or related to other content on that page but it still hides form 3 if I tell it to hide form 1.

Does anyone have any idea why the form id would not work?

CodePudding user response:

Does anyone have any idea why the form id would not work?

Because you added your filter in a way, that it only applies for the form with id 3 to begin with.

https://docs.gravityforms.com/gform_get_form_filter/#h-usage:

Applies to all forms:
add_filter( 'gform_get_form_filter', 'your_function_name', 10, 2 );

To target a specific form append the form id to the hook name. (format: gform_get_form_filter_FORMID)
add_filter( 'gform_get_form_filter_10', 'your_function_name', 10, 2 );

You chose the second version here: add_filter( 'gform_get_form_filter_3', ...) - so it will only ever manipulate the output for form 3.

  • Related