Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zackkatz/7ada522a150c42626a6dd07c6203ea1e to your computer and use it in GitHub Desktop.
Save zackkatz/7ada522a150c42626a6dd07c6203ea1e to your computer and use it in GitHub Desktop.
GravityView: Show the result of a calculation using a Custom Content field
<?php
/**
* Referenced in this article: http://docs.gravityview.co/article/235-how-to-make-calculations-using-the-custom-content-field
*/
/** Modify the content returned from the Custom Content field */
add_filter( 'gravityview/fields/custom/content_before', 'my_gv_custom_content_before', 10, 2 );
/**
* Replaces the %CALC% placeholder by the result of a calc operation between entries' fields
*
* @param string $content Custom Content field content
* @param \GV\Template_Context $context
* @return string
*/
function my_gv_custom_content_before( $content, $context = null ) {
// this is the tag used inside the Custom Content Field
$tag = '%CALC%';
// Is the placeholder tag in the Custom Content field? If not, return original value.
if( false === strpos( $content, $tag ) ) {
return $content;
}
$replace = '';
// If the fields used for the calculation are set, replace the tag with the calculation.
if( isset( $context->entry['15'] ) && isset( $context->entry['37'] ) ) {
$replace = $context->entry['15'] + $context->entry['37'];
}
return str_replace( $tag, $replace, $content );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment