Skip to content

Instantly share code, notes, and snippets.

@ThemeCatcher
Last active November 22, 2023 07:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThemeCatcher/2c400c6ca2d37dbbaa3b64b2bb2cc1eb to your computer and use it in GitHub Desktop.
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
<?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