Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save adczk/f46b9545176bcb181830d8cc2329183c to your computer and use it in GitHub Desktop.

Select an option

Save adczk/f46b9545176bcb181830d8cc2329183c to your computer and use it in GitHub Desktop.
Forminator - save submission id (entry id) in custom field upon post creation
<?php
/*
Forminator - save submission ID in posts custom field upon new post creation
This is only post creation forms
Tested with Forminator 1.22.1
by adamcz/WPMU DEV
USE AS MU PLUGIN
Config explained in comments
*/
// get the entry ID into temporary $_POST key (as otherwise it's not available)
add_action( 'forminator_custom_form_submit_before_set_fields', 'forminator_customfield_entry_id', 20, 3 );
function forminator_customfield_entry_id( $entry, $form_id, $field_data_array ) {
$_POST['_forminator_cform_entry_id'] = $entry->entry_id;
$_POST['_forminator_cform_form_id'] = $form_id;
}
add_action( 'forminator_form_after_save_entry', 'forminator_customfield_entryid_set', 10, 2 );
function forminator_customfield_entryid_set( $form_id, $response ) {
/* CONFIGURE HERE */
$forms = array( 2297 ); // comma separated IDs of forms this should work with
$postdata_field = 'postdata-1'; // field ID of post data field
$customfield_id = 'MYCUSTOM'; // set the name of your custom field to be updated
/* CONFIGURATION END */
if ( ! in_array( $form_id, $forms ) ) {
return;
}
if ( $response && is_array( $response ) ) {
if ( $response['success'] ) {
if ( $form_id == $_POST['_forminator_cform_form_id'] ) {
// get entry ID
$entry_id = $_POST['_forminator_cform_entry_id'];
// fetch entry data
$entry = Forminator_API::get_entry( $form_id, $entry_id );
// get new post ID
$postid = $entry->meta_data[$postdata_field]['value']['postdata'];
update_post_meta( $postid, $customfield_id, $entry_id );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment