Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.