Save Contact Form 7 Submissions in the Admin
Keeps a copy of every Contact Form 7 message in a private "Form Messages" list in the admin, so nothing is lost when an email goes missing.
Opens WPIDE on
Code
add_action( 'init', function () {
register_post_type( 'wpide_cf7_message', array(
'labels' => array( 'name' => 'Form Messages', 'singular_name' => 'Form Message', 'menu_name' => 'Form Messages' ),
'public' => false,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-email-alt',
'supports' => array( 'title', 'editor' ),
'capability_type' => 'post',
'capabilities' => array( 'create_posts' => 'do_not_allow' ),
'map_meta_cap' => true,
) );
} );
add_action( 'wpcf7_mail_sent', function ( $contact_form ) {
$submission = class_exists( 'WPCF7_Submission' ) ? WPCF7_Submission::get_instance() : null;
if ( ! $submission ) {
return;
}
$lines = array();
foreach ( (array) $submission->get_posted_data() as $key => $value ) {
if ( 0 === strpos( $key, '_' ) ) {
continue;
}
$lines[] = '<strong>' . esc_html( $key ) . ':</strong> ' . nl2br( esc_html( is_array( $value ) ? implode( ', ', $value ) : (string) $value ) );
}
$lines[] = '<strong>Page:</strong> ' . esc_url( (string) $submission->get_meta( 'url' ) );
wp_insert_post( array(
'post_type' => 'wpide_cf7_message',
'post_status' => 'private',
'post_title' => sprintf( '%s – %s', $contact_form->title(), wp_date( 'Y-m-d H:i' ) ),
'post_content' => wp_kses_post( implode( "<br>\n", $lines ) ),
'meta_input' => array( '_wpide_cf7_form_id' => (int) $contact_form->id() ),
) );
} );
Notes
Messages are stored as private posts that only administrators can see (file uploads are not kept), so delete old ones from the list from time to time.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.