Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active August 18, 2019 23:31
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 xnau/1d66ca8e327e1655b32d69578c0e9e0b to your computer and use it in GitHub Desktop.
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.
<?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;
}
@xnau
Copy link
Author

xnau commented Aug 18, 2019

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

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