We have a petition and we would like the names to show automatically in a landing page. Is there a code we can use to embed in a landing page that collates the names and displays it?
Hello. You can use this code to create a shortcode called gf_name_list
that will output data from your entries.
// Usage: [gf_name_list form_id="134" field_ids="1.3,1.6"]
function gf_output_shortcode( $atts ) {
// Define default attributes and merge with user-defined attributes
$atts = shortcode_atts(
array(
'form_id' => 134, // Default form ID, can be overridden via the shortcode
'field_ids' => '1.3', // Default field ID, can be overridden via the shortcode
),
$atts,
'gf_name_list'
);
// Check if Gravity Forms API is available
if ( ! class_exists( 'GFAPI' ) ) {
return 'Gravity Forms is not installed.';
}
// Get the form ID from the shortcode attributes
$form_id = intval( $atts['form_id'] );
// Get the field IDs from the shortcode attributes
$field_ids = explode( ',', $atts['field_ids'] );
// Get all entries for the specified form
$entries = GFAPI::get_entries( $form_id );
// Initialize the output string
$output = '<ul>';
// Loop through each entry
foreach ( $entries as $entry ) {
// Initialize an array to hold field values
$field_values = array();
// Loop through each specified field ID
foreach ( $field_ids as $field_id ) {
// Ensure no extra spaces
$field_id = trim( $field_id );
// Get the field value
$value = rgar( $entry, $field_id );
if ( $value ) {
// Add the value to the array
$field_values[] = esc_html( $value );
}
}
// Combine all field values into one string and output them in one <li>
if ( ! empty( $field_values ) ) {
$output .= '<li>' . implode( ' ', $field_values ) . '</li>';
}
}
$output .= '</ul>';
return $output;
}
// Register the shortcode
add_shortcode( 'gf_name_list', 'gf_output_shortcode' );
You can use the shortcode in a page by adding a shortcode block. Then add the shortcode like this:
[gf_name_list form_id="134" field_ids="1.3,1.6"]
The shortcode will accept the form ID as a parameter, and a comma separated list of field IDs as a parameter. Here, I am pulling the name field ID 1 first and last names from all the entries for form 134.
The field IDs you pass in will be output separated by a space. Sample output from some entries in a form on my site:
Let me know if you have any other questions.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.