To enable front-end form submissions in WordPress using WP Forms, you can use the wp_insert_post() function along with the wp_handle_upload() function to handle file uploads. Here’s an example code snippet that demonstrates how to achieve this:
function wpsnippets_submit_form() {
if ( isset( $_POST['submit_form'] ) ) {
// Create a new post
$post_data = array(
'post_title' => sanitize_text_field( $_POST['form_title'] ),
'post_content' => sanitize_textarea_field( $_POST['form_content'] ),
'post_status' => 'publish',
'post_type' => 'post',
);
$post_id = wp_insert_post( $post_data );
// Handle file upload
if ( ! empty( $_FILES['form_file']['name'] ) ) {
$file = $_FILES['form_file'];
$upload_overrides = array( 'test_form' => false );
$uploaded_file = wp_handle_upload( $file, $upload_overrides );
if ( isset( $uploaded_file['file'] ) ) {
$attachment = array(
'post_mime_type' => $uploaded_file['type'],
'post_title' => sanitize_file_name( $uploaded_file['file'] ),
'post_content' => '',
'post_status' => 'inherit',
'post_parent' => $post_id,
);
$attachment_id = wp_insert_attachment( $attachment, $uploaded_file['file'], $post_id );
if ( ! is_wp_error( $attachment_id ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file['file'] );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
}
}
}
}
}
add_action( 'init', 'wpsnippets_submit_form' );
This code snippet demonstrates a function wpsnippets_submit_form() that is hooked to the init action. It checks if the form has been submitted and then creates a new post using the wp_insert_post() function. It also handles file uploads using the wp_handle_upload() function and attaches the uploaded file to the newly created post using the wp_insert_attachment() function.
Examples
Example 1: Creating a Custom Front-End Form for User Submissions
This example demonstrates how to create a custom front-end form using WP Forms plugin to allow users to submit content from the front-end of your WordPress site.
function wpsnippets_custom_submission_form() {
ob_start();
wpforms_display( 1 );
return ob_get_clean();
}
add_shortcode( 'custom_submission_form', 'wpsnippets_custom_submission_form' );
In this code example, we create a custom shortcode custom_submission_form that can be used to display a WP Forms form on the front-end of the site. The wpforms_display() function is used to render the form with the ID 1. Users can then use the [custom_submission_form] shortcode to display the form on any page or post.
Example 2: Validating and Processing Front-End Form Submissions
This example demonstrates how to validate and process form submissions from the front-end using a custom PHP function.
function wpsnippets_process_submission() {
if ( isset( $_POST['submit_form'] ) ) {
// Perform form validation and processing here
// ...
// Redirect or display success message
}
}
add_action( 'init', 'wpsnippets_process_submission' );
In this code example, we use the init action hook to call the wpsnippets_process_submission() function when the page is loaded. Inside the function, we check if the form has been submitted by checking the presence of the submit_form field in the $_POST superglobal. You can then perform any necessary form validation and processing before redirecting the user or displaying a success message.
Example 3: Sending Email Notifications for Front-End Form Submissions
This example demonstrates how to send email notifications to site administrators when a form is submitted from the front-end.
function wpsnippets_send_notification( $entry, $form_data ) {
$to = get_option( 'admin_email' );
$subject = 'New Form Submission';
$message = 'A new form submission has been received.';
wp_mail( $to, $subject, $message );
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_notification', 10, 2 );
In this code example, we use the wpforms_process_complete action hook to call the wpsnippets_send_notification() function when a form submission is processed. The function receives two parameters: $entry, which contains the form submission data, and $form_data, which contains information about the form itself. Inside the function, we retrieve the site administrator’s email address using get_option( 'admin_email' ) and send an email notification using the wp_mail() function.
