Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active October 4, 2017 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/ceb4d2b6cbe7660263339d060f8268b9 to your computer and use it in GitHub Desktop.
Save Shelob9/ceb4d2b6cbe7660263339d060f8268b9 to your computer and use it in GitHub Desktop.
<?php
/**
* Get total value of all entries in a Caldera Forms field by ID
*/
global $wpdb;
$table = $wpdb->prefix . 'cf_form_entry_values';
//IMPORTANT - change field ID to your field's ID
$r = $wpdb->get_results( "SELECT SUM( `value` ) FROM $table WHERE `field_id` = 'fld_9727773'", ARRAY_A );
if( isset( $r[0], $r[0]['SUM( `value` )'])){
$total = $r[0]['SUM( `value` )'];
}else{
$total = 0;
}
[my_cf_shortcode form="cf123456789" field="fld_9727773"]
<?php
/**
* Create a custom shortcode to display total value of a field's entries above the form
*/
add_shortcode( 'my_cf_shortcode', function( $atts ){
$args = shortcode_atts( array(
'form' => null,
'field' => null,
), $atts );
if( empty( $args[ 'form' ] ) || empty( $args[ 'field' ] ) ){
return;
}
$field = caldera_forms_very_safe_string( $args[ 'field' ] );
global $wpdb;
$table = $wpdb->prefix . 'cf_form_entry_values';
$r = $wpdb->get_results( $wpdb->prepare( "SELECT SUM( `value` ) FROM $table WHERE `field_id` = '%s'", $field ), ARRAY_A );
if( isset( $r[0], $r[0]['SUM( `value` )'])){
$total = $r[0]['SUM( `value` )'];
}else{
$total = 0;
}
//IMPORTANT - Customize display of total here -
$out = '<div class="my-cf-total-display">So far the total raised is ' . $total . '</div>';
$out .= Caldera_Forms::render_form( $args[ 'form' ] );
return $out;
});
@shawnhooper
Copy link

shawnhooper commented Oct 4, 2017

global $wpdb;

$total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT IFNULL(SUM(value),0) FROM {$wpdb->prefix}cf_form_entry_values} WHERE field_id = '%s'", caldera_forms_very_safe_string( $args[ 'field' ] ) ) );

Should do it.

You can replace lines 13 - 21 with the above 2 lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment