Created
June 11, 2019 11:07
-
-
Save New0/49eea85e37fa945b02c37420e61e3a21 to your computer and use it in GitHub Desktop.
Save CF new form entries as user meta data and display a list of these entries linked to a modal form filled with entry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: CF entries as user meta data | |
* Description: Save entries as USER metadata, display list of entries for a user to see / edit them | |
* Author: New0 | |
*/ | |
/** | |
* On form submit, associate new entries to USER | |
*/ | |
add_action( 'caldera_forms_submit_complete', function( $form, $form_link_array, $process_id, $entry_id ){ | |
//change form ID to match your form | |
if( 'CF5cae2cff5d65c' === $form[ 'ID' ] && get_current_user_id() ){ | |
$form_entry_ids_user_meta = get_user_meta( get_current_user_id(), 'form_entry_ids' ); | |
if(!empty($form_entry_ids_user_meta)){ | |
$entry_ids = $form_entry_ids_user_meta[0]; | |
} | |
$entry_ids[] = $entry_id; | |
update_user_meta( get_current_user_id(), 'form_entry_ids', array_unique($entry_ids) ); | |
} | |
}, 10, 4); | |
/** | |
* Adds the list of entries associated to the current user in the POST with ID 165 (edit with post ID needed) | |
*/ | |
add_filter( 'the_content', function( $content ){ | |
$post = get_post(); | |
//EDIT POST ID | |
if( 165 === $post->ID ){ | |
//EDIT FORM ID | |
$form = 'CF5cae2cff5d65c'; | |
$entry_ids = get_user_meta( get_current_user_id(), 'form_entry_ids' ); | |
if(!empty($entry_ids)){ | |
$entry_ids = $entry_ids[0]; | |
} | |
$view_entries = '<div class="my-custom-entries"><ul>'; | |
foreach($entry_ids as $key => $entry_id){ | |
$entry = Caldera_Forms::get_entry($entry_id, $form); | |
if( $entry['status'] === "active" ){ | |
$view_entries .= '<li>' . do_shortcode('[caldera_form_modal id="'. $form . '" entry="' . $entry_id . '"] | |
View / Edit entry '. $entry_id . ' | |
[/caldera_form_modal]') . '</li>'; | |
} | |
} | |
$view_entries .= '</ul></div>'; | |
$content .= $view_entries; | |
} | |
return $content; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment