Last updated on October 18, 2023

WP Forms entry approval workflow

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

Create workflows for entry approval in WP Forms.

The code snippet below demonstrates how to create a custom approval workflow for WP Forms entries in WordPress. This workflow allows administrators to review and approve form submissions before they are published or processed further.

/**
 * Add custom status for WP Forms entries.
 */
function wpsnippets_add_custom_entry_status() {
    register_post_status( 'pending_approval', array(
        'label'                     => _x( 'Pending Approval', 'WP Forms entry status', 'text-domain' ),
        'public'                    => false,
        'exclude_from_search'       => true,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Pending Approval <span class="count">(%s)</span>', 'Pending Approval <span class="count">(%s)</span>', 'text-domain' )
    ) );
}
add_action( 'init', 'wpsnippets_add_custom_entry_status' );

/**
 * Add custom status to WP Forms entry filter dropdown.
 *
 * @param array $statuses
 * @return array
 */
function wpsnippets_add_custom_entry_status_filter( $statuses ) {
    $statuses['pending_approval'] = _x( 'Pending Approval', 'WP Forms entry status', 'text-domain' );
    return $statuses;
}
add_filter( 'wpforms_entry_statuses', 'wpsnippets_add_custom_entry_status_filter' );

/**
 * Set default status for new WP Forms entries.
 *
 * @param string $status
 * @param array  $form_data
 * @return string
 */
function wpsnippets_set_default_entry_status( $status, $form_data ) {
    if ( empty( $status ) && isset( $form_data['id'] ) ) {
        $status = 'pending_approval';
    }
    return $status;
}
add_filter( 'wpforms_entry_default_status', 'wpsnippets_set_default_entry_status', 10, 2 );

This code snippet adds a custom entry status called “Pending Approval” for WP Forms entries. The wpsnippets_add_custom_entry_status() function registers the new status using the register_post_status() function. The wpsnippets_add_custom_entry_status_filter() function adds the new status to the WP Forms entry filter dropdown. Finally, the wpsnippets_set_default_entry_status() function sets the default status for new entries to “Pending Approval”.

This code can be useful in scenarios where you want to implement an approval process for form submissions, allowing administrators to review and approve entries before they are published or processed further.

Examples

Example 1: Creating a custom status for WP Forms entries

This use case demonstrates how to create a custom status for WP Forms entries using the wpsnippets_create_custom_entry_status() function. This function registers a new entry status and adds it to the list of available statuses in WP Forms.

/**
 * Create a custom status for WP Forms entries.
 */
function wpsnippets_create_custom_entry_status() {
    register_post_status( 'custom_status', array(
        'label'                     => _x( 'Custom Status', 'WP Forms entry status', 'text-domain' ),
        'public'                    => true,
        'exclude_from_search'       => true,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Custom Status <span class="count">(%s)</span>', 'Custom Status <span class="count">(%s)</span>', 'text-domain' ),
    ) );
}
add_action( 'init', 'wpsnippets_create_custom_entry_status' );

In this code example, we use the register_post_status() function to create a new status called ‘custom_status’ for WP Forms entries. We specify various parameters such as the label, visibility, and count label for the status. The add_action() function is used to hook this code to the ‘init’ action, ensuring that the custom status is registered during the initialization of WordPress.

Example 2: Changing the default status of WP Forms entries

This use case demonstrates how to change the default status of WP Forms entries using the wpsnippets_change_default_entry_status() function. This function modifies the default status of new entries created through WP Forms.

/**
 * Change the default status of WP Forms entries.
 */
function wpsnippets_change_default_entry_status( $status, $form_id ) {
    if ( $form_id === 123 ) {
        $status = 'custom_status';
    }
    return $status;
}
add_filter( 'wpforms_entry_default_status', 'wpsnippets_change_default_entry_status', 10, 2 );

In this code example, we use the wpforms_entry_default_status filter to change the default status of WP Forms entries. The wpsnippets_change_default_entry_status() function checks if the form ID is equal to 123, and if so, sets the status to ‘custom_status’. This allows you to customize the default status based on the form ID.

Example 3: Displaying entry status in WP Forms admin area

This use case demonstrates how to display the entry status in the WP Forms admin area using the wpsnippets_display_entry_status() function. This function adds a new column to the WP Forms entries table and displays the status of each entry.

/**
 * Display entry status in WP Forms admin area.
 */
function wpsnippets_display_entry_status( $columns ) {
    $columns['entry_status'] = __( 'Status', 'text-domain' );
    return $columns;
}
add_filter( 'wpforms_admin_entries_columns', 'wpsnippets_display_entry_status' );

function wpsnippets_display_entry_status_content( $column, $entry ) {
    if ( $column === 'entry_status' ) {
        $status = get_post_status( $entry->ID );
        echo esc_html( $status );
    }
}
add_action( 'wpforms_admin_entries_custom_column', 'wpsnippets_display_entry_status_content', 10, 2 );

In this code example, we use the wpforms_admin_entries_columns filter to add a new column called ‘Status’ to the WP Forms entries table. The wpsnippets_display_entry_status_content() function is hooked to the wpforms_admin_entries_custom_column action and is responsible for displaying the status of each entry in the ‘Status’ column. We retrieve the entry status using the get_post_status() function and output it using esc_html() to ensure proper sanitization.

Last updated on October 18, 2023. Originally posted on October 24, 2023.

Leave a Reply

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