Last active
October 4, 2017 23:48
-
-
Save Shelob9/ceb4d2b6cbe7660263339d060f8268b9 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* 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; | |
} |
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
[my_cf_shortcode form="cf123456789" field="fld_9727773"] |
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 | |
/** | |
* 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; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.