Last updated on October 18, 2023

WP Forms conditional form expiration

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Set form expiration based on conditions.

The code snippet provided below demonstrates how to implement conditional form expiration in WP Forms. This functionality can be useful when you want to set an expiration date for a form and automatically disable it after that date.

To achieve this, you can use the wpsnippets_wpforms_conditional_form_expiration() function. This function checks the current date against the expiration date set for the form and disables the form if the expiration date has passed.

/**
 * Conditional form expiration for WP Forms
 */
function wpsnippets_wpforms_conditional_form_expiration( $form_data, $form_id ) {
    // Set the expiration date for the form
    $expiration_date = '2022-12-31';

    // Get the current date
    $current_date = date( 'Y-m-d' );

    // Disable the form if the expiration date has passed
    if ( $current_date > $expiration_date ) {
        $form_data['settings']['form_status'] = 'inactive';
    }

    return $form_data;
}
add_filter( 'wpforms_frontend_form_data', 'wpsnippets_wpforms_conditional_form_expiration', 10, 2 );

In the code snippet above, we define the wpsnippets_wpforms_conditional_form_expiration() function that takes two parameters: $form_data and $form_id. The $form_data parameter contains the form settings and data, while the $form_id parameter represents the ID of the form.

Inside the function, we set the $expiration_date variable to the desired expiration date for the form. You can modify this value to match your requirements.

Next, we retrieve the current date using the date() function and format it as 'Y-m-d'.

Then, we compare the current date with the expiration date using a simple comparison operator (>). If the current date is greater than the expiration date, we update the form status to 'inactive' by modifying the $form_data array.

Finally, we return the updated $form_data array.

By adding this code snippet to your WordPress theme’s functions.php file or a custom plugin, you can easily implement conditional form expiration for WP Forms.

Examples

Example 1: Setting a conditional form expiration date

This use case demonstrates how to set a conditional expiration date for a WP Forms form. The code example below shows how to add a custom function that checks the current date against a specified expiration date, and disables the form if the expiration date has passed.

function wpsnippets_set_form_expiration_date( $form_id ) {
    $expiration_date = '2022-12-31'; // Set the expiration date here
    $current_date = date( 'Y-m-d' );

    if ( $current_date > $expiration_date ) {
        wpforms()->form->disable( $form_id );
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_set_form_expiration_date' );

In this code example, we define a custom function wpsnippets_set_form_expiration_date that takes the form ID as a parameter. Inside the function, we set the expiration date and retrieve the current date. We then compare the two dates and disable the form using the wpforms()->form->disable() method if the expiration date has passed. Finally, we hook this function to the wpforms_process_complete action to execute it when a form is submitted.

Example 2: Displaying a message when the form has expired

This use case demonstrates how to display a custom message when a WP Forms form has expired. The code example below shows how to modify the form’s confirmation message based on the expiration status.

function wpsnippets_display_expired_form_message( $confirmation, $form_data ) {
    $expiration_date = '2022-12-31'; // Set the expiration date here
    $current_date = date( 'Y-m-d' );

    if ( $current_date > $expiration_date ) {
        $confirmation['message'] = 'This form has expired.';
    }

    return $confirmation;
}
add_filter( 'wpforms_frontend_form_confirmation', 'wpsnippets_display_expired_form_message', 10, 2 );

In this code example, we define a custom function wpsnippets_display_expired_form_message that takes the form’s confirmation message and form data as parameters. Inside the function, we set the expiration date and retrieve the current date. We then compare the two dates and modify the confirmation message to display a custom message if the expiration date has passed. Finally, we hook this function to the wpforms_frontend_form_confirmation filter to apply the modification.

Example 3: Redirecting to a specific page when the form has expired

This use case demonstrates how to redirect users to a specific page when a WP Forms form has expired. The code example below shows how to modify the form’s redirect URL based on the expiration status.

function wpsnippets_redirect_expired_form( $url, $form_data ) {
    $expiration_date = '2022-12-31'; // Set the expiration date here
    $current_date = date( 'Y-m-d' );

    if ( $current_date > $expiration_date ) {
        $url = 'https://example.com/expired-page'; // Set the redirect URL here
    }

    return $url;
}
add_filter( 'wpforms_frontend_form_redirect_url', 'wpsnippets_redirect_expired_form', 10, 2 );

In this code example, we define a custom function wpsnippets_redirect_expired_form that takes the form’s redirect URL and form data as parameters. Inside the function, we set the expiration date and retrieve the current date. We then compare the two dates and modify the redirect URL to a specific page if the expiration date has passed. Finally, we hook this function to the wpforms_frontend_form_redirect_url filter to apply the modification.

Last updated on October 18, 2023. Originally posted on December 31, 2023.

Leave a Reply