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 Nov 28, 2018

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.

@drflax
Copy link

drflax commented Aug 18, 2019

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

@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