Skip to content

Instantly share code, notes, and snippets.

@luistinygod
Last active November 7, 2016 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save luistinygod/7594392d3a474cf93df0 to your computer and use it in GitHub Desktop.
Save luistinygod/7594392d3a474cf93df0 to your computer and use it in GitHub Desktop.
GravityView: Show the result of a calculation using a Custom Content field
<?php
/** Modify the content returned from the Custom Content field */
add_filter( 'gravityview/fields/custom/content_before', 'my_gv_custom_content_before', 10, 1 );
/**
* Replaces the %CALC% placeholder by the result of a calc operation between entries' fields
*
* @param string $content Custom Content field content
* @return string
*/
function my_gv_custom_content_before( $content ) {
// this is the tag used inside the Custom Content Field
$tag = '%CALC%';
// Checks if this custom content field is the one we will use to perform calculations
if( false === strpos( $content, $tag ) ) {
return $content;
}
// Remove the placeholder from the field content
$content = str_replace( $tag, '', $content );
global $gravityview_view;
extract( $gravityview_view->field_data );
if( isset( $entry['15'] ) && isset( $entry['37'] ) ) {
// sums up field #15 with field #37
return $entry['15'] + $entry['37'];
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment