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; | |
} |
can you explain where this code goes, so i',ve tried on the theme funcions and in separate file on the plugin folder and no way.Thx in advance
This code is a plugin, take a look at this article which explains how to install it:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is no error checking of any kind, so be careful to use the correct field names, also they must be numeric in order for the totaling to work.