Last active
August 18, 2019 23:31
-
-
Save xnau/1d66ca8e327e1655b32d69578c0e9e0b to your computer and use it in GitHub Desktop.
Shows how to set up a Participants Database field that keeps a total of several other fields.
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 | |
/* | |
Plugin Name: Participants Database Total Field | |
Description: Maintains a total of several fields | |
*/ | |
// this updates the total when the record is updated | |
add_filter( 'pdb-before_submit_update', 'xnau_update_total_field' ); | |
/** | |
* updates the total field | |
* | |
* @param array $record the submitted record data | |
* @return array the data to store | |
*/ | |
function xnau_update_total_field ( $record ) | |
{ | |
// set this up to list the fields to sum | |
// must be numeric! | |
$fields_to_total = array('numeric_1','numeric_2'); | |
// name of the total field | |
// should be numeric field | |
$total_field = 'numeric_sum'; | |
// calculate the total | |
$total = 0; | |
foreach ( $fields_to_total as $field ) { | |
$total += $record[$field]; | |
} | |
// place the total in the data array | |
$record[$total_field] = $total; | |
// return the record data to be saved | |
return $record; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is a plugin, take a look at this article which explains how to install it:
How to Install a WordPress Plugin from a Gist