-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgravityforms-total-amount-shortcode.php
55 lines (46 loc) · 1.58 KB
/
gravityforms-total-amount-shortcode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/*
* Plugin Name: Gravity Forms Total Amount Shortcode
* Plugin URI: https://github.com/prontotools/gravityforms-total-amount-shortcode
* Description: A simple shortcode that displays the “Total” filed value from any Gravity Form.
* Version: 1.0.0
* Author: Pronto Tools
* Author URI: http://www.prontotools.io
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
function gravityforms_total_amount_shortcode( $atts ) {
$defaults = array(
'form_id' => '',
'start_date' => '',
'end_date' => '',
'field_id' => ''
);
$atts = shortcode_atts( $defaults, $atts );
$start_date = null;
$end_date = null;
$entries = '';
$total = 0;
if ( $atts['start_date'] ) {
$start_date = date( 'Y-m-d', strtotime( $atts['start_date'] ) );
}
if ( $atts['end_date'] ) {
$end_date = date( 'Y-m-d', strtotime( $atts['end_date'] ) );
}
$search_criteria = array();
if ( $atts['form_id'] ) {
if ( $start_date or $end_date ) {
$search_criteria['start_date'] = $start_date;
$search_criteria['end_date'] = $end_date;
}
$entries = GFAPI::get_entries( $atts['form_id'], $search_criteria );
if ( $atts['field_id'] ) {
foreach ( $entries as $entry ) {
$total_field = $entry[ $atts['field_id'] ];
$total += $total_field;
}
}
}
return $total;
}
add_shortcode( 'gravityform-total-amount', 'gravityforms_total_amount_shortcode' );