Last updated on October 18, 2023

WP Forms conditional approval

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

Approve submissions based on conditions.

To implement conditional approval for WP Forms, you can use the wpforms_process_entry hook to check the form submission and conditionally approve or reject it based on your custom logic. Here’s an example code snippet that demonstrates how to achieve this:

/**
 * Conditional approval for WP Forms.
 *
 * @param array $fields    The form fields.
 * @param array $entry     The form entry data.
 * @param array $form_data The form data.
 *
 * @return array
 */
function wpsnippets_wpforms_conditional_approval( $fields, $entry, $form_data ) {
    // Add your custom logic here to determine if the form submission should be approved or rejected.
    // For example, you can check the value of a specific field or perform any other checks.

    // If the form submission should be approved, set the 'status' field to 'approved'.
    $fields['status'] = 'approved';

    // If the form submission should be rejected, set the 'status' field to 'rejected'.
    // $fields['status'] = 'rejected';

    return $fields;
}
add_filter( 'wpforms_process_entry', 'wpsnippets_wpforms_conditional_approval', 10, 3 );

In this code snippet, we define a custom function wpsnippets_wpforms_conditional_approval that hooks into the wpforms_process_entry filter. This filter is triggered when a form submission is processed.

Inside the function, you can add your custom logic to determine whether the form submission should be approved or rejected. In the example, we check the value of a hypothetical field called ‘status’ and set it to ‘approved’ or ‘rejected’ accordingly.

Finally, we return the modified $fields array, which contains the form field values. The modified ‘status’ field will determine the approval status of the form submission.

You can customize this code snippet to fit your specific conditional approval requirements by modifying the logic inside the function.

Examples

Example 1: Conditional Approval Based on User Role

This use case demonstrates how to implement conditional approval for WP Forms based on the user role. The code example checks if the user has a specific role and approves or rejects the form submission accordingly.

function wpsnippets_conditional_approval_user_role( $is_approved, $form_id, $entry ) {
    $user = wp_get_current_user();

    if ( in_array( 'editor', (array) $user->roles ) ) {
        $is_approved = true;
    } else {
        $is_approved = false;
    }

    return $is_approved;
}
add_filter( 'wpforms_process_entry_approval', 'wpsnippets_conditional_approval_user_role', 10, 3 );

In this code example, we use the wpforms_process_entry_approval filter to modify the approval status of a form submission. We check if the current user has the “editor” role using wp_get_current_user() and in_array(). If the user has the “editor” role, the form submission is approved; otherwise, it is rejected.

Example 2: Conditional Approval Based on Form Field Value

This use case demonstrates how to implement conditional approval for WP Forms based on the value of a specific form field. The code example checks if a form field has a specific value and approves or rejects the form submission accordingly.

function wpsnippets_conditional_approval_field_value( $is_approved, $form_id, $entry ) {
    $field_value = wpforms()->entry->get_meta( $entry['id'], 'field_id' );

    if ( $field_value === 'approved' ) {
        $is_approved = true;
    } else {
        $is_approved = false;
    }

    return $is_approved;
}
add_filter( 'wpforms_process_entry_approval', 'wpsnippets_conditional_approval_field_value', 10, 3 );

In this code example, we use the wpforms_process_entry_approval filter to modify the approval status of a form submission. We retrieve the value of a specific form field using wpforms()->entry->get_meta() and compare it with the desired value. If the field value is “approved”, the form submission is approved; otherwise, it is rejected.

Example 3: Conditional Approval Based on Custom Logic

This use case demonstrates how to implement conditional approval for WP Forms based on custom logic. The code example checks the form submission data and applies custom logic to determine whether to approve or reject the submission.

function wpsnippets_conditional_approval_custom_logic( $is_approved, $form_id, $entry ) {
    $field_value1 = wpforms()->entry->get_meta( $entry['id'], 'field_id1' );
    $field_value2 = wpforms()->entry->get_meta( $entry['id'], 'field_id2' );

    if ( $field_value1 === 'approved' && $field_value2 > 10 ) {
        $is_approved = true;
    } else {
        $is_approved = false;
    }

    return $is_approved;
}
add_filter( 'wpforms_process_entry_approval', 'wpsnippets_conditional_approval_custom_logic', 10, 3 );

In this code example, we use the wpforms_process_entry_approval filter to modify the approval status of a form submission. We retrieve the values of two form fields using wpforms()->entry->get_meta() and apply custom logic to determine whether the submission should be approved or rejected. In this example, the submission is approved if the value of “fieldid1″ is “approved” and the value of “fieldid2″ is greater than 10.

Last updated on October 18, 2023. Originally posted on November 6, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *