Last active
November 7, 2016 18:04
-
-
Save luistinygod/7594392d3a474cf93df0 to your computer and use it in GitHub Desktop.
GravityView: Show the result of a calculation using a Custom Content field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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