Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Created March 25, 2020 06:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save finalwebsites/4c9eb0da817cfc360637f52ac38b2f90 to your computer and use it in GitHub Desktop.
Save finalwebsites/4c9eb0da817cfc360637f52ac38b2f90 to your computer and use it in GitHub Desktop.
Store Elementor form submissions using a custom post type
<?php
// place this code into you child theme's functions.php file
// Do you use Advanced Custom Fields? Uncomment the next row, otherwise the custom fields meta box doesn't show up in your edit post screen
//add_filter('acf/settings/remove_wp_meta_box', '__return_false');
add_action( 'init', 'fws_elementor_submissions_post_type' );
function fws_elementor_submissions_post_type() {
$args = array(
'public' => false,
'show_ui' => true,
'label' => __( 'Form submissions', 'textdomain' ),
'menu_icon' => 'dashicons-feedback',
'supports' => array( 'title', 'custom-fields' ),
'capabilities' => array( 'create_posts' => false ),
'map_meta_cap' => true
);
register_post_type( 'elem_forms_submit', $args );
}
add_action( 'elementor_pro/forms/new_record', function( $record, $handler = null ) {
$subject = $record->get_form_settings( 'email_subject' );
$my_post = array(
'post_title' => $subject,
'post_type' => 'elem_forms_submit',
'post_status' => 'publish',
'post_author' => 1
);
if ($post_id = wp_insert_post( $my_post )) {
$raw_fields = $record->get( 'fields' );
foreach ( $raw_fields as $id => $field ) {
update_post_meta( $post_id, $id, $field['value'] );
}
}
});
@finalwebsites
Copy link
Author

Form submissions are stored now by Elementor.
Tip! Use this code example (with some changes) if you need to build a front-end submission function with an Elementor form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment