Last active
November 22, 2023 07:59
-
-
Save ThemeCatcher/2c400c6ca2d37dbbaa3b64b2bb2cc1eb to your computer and use it in GitHub Desktop.
Shortcode to show current user entries for a specific form using [show_user_entries id="1"] where 1 is the form ID
This file contains 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: Quform Show User Entries | |
* Plugin URI: http://www.quform.com | |
* Description: Shortcode to show current user entries. | |
* Version: 1.1.1 | |
* Author: ThemeCatcher | |
* Author URI: http://www.themecatcher.net | |
*/ | |
add_shortcode('show_user_entries', function ($atts) { | |
$atts = shortcode_atts(array( | |
'id' => '' | |
), $atts); | |
if ( ! is_user_logged_in()) { | |
return '<p>Please log in to view entries.</p>'; | |
} | |
$repository = Quform::getService('repository'); | |
$formFactory = Quform::getService('formFactory'); | |
$config = $repository->getConfig((int) $atts['id']); | |
if ( ! is_array($config)) { | |
return '<p>Form not found.</p>'; | |
} | |
$output = ''; | |
$form = $formFactory->create($config); | |
$entries = $repository->getEntries($form, array( | |
'order_by' => 'created_at', | |
'order' => 'ASC', | |
'limit' => 1000000, | |
'created_by' => get_current_user_id() | |
)); | |
if (is_array($entries) && count($entries)) { | |
foreach ($entries as $entry) { | |
sue_set_form_values($form, $entry); | |
$output .= $form->replaceVariables('{all_form_data}', 'html'); | |
} | |
} else { | |
$output .= '<p>No entries found.</p>'; | |
} | |
return $output; | |
}); | |
function sue_set_form_values(Quform_Form $form, array $entry) | |
{ | |
$form->setValues(array(), true); | |
foreach ($entry as $key => $value) { | |
if (preg_match('/^element_\d+/', $key)) { | |
$elementId = (int) str_replace('element_', '', $key); | |
$form->setValueFromStorage($elementId, $entry[$key]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment