Skip to content

Instantly share code, notes, and snippets.

@Idealien
Last active December 11, 2017 08:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Idealien/ca3a3e86ede360c3e2c1 to your computer and use it in GitHub Desktop.
Save Idealien/ca3a3e86ede360c3e2c1 to your computer and use it in GitHub Desktop.
Gravity Forms - Tally Common Questions in Merge Tag
<?php
add_filter( 'gform_replace_merge_tags', array(&$this, "replace_tally_results"), 10, 7 );
function replace_tally_results( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
$custom_merge_tag = '{tally_results}';
if ( strpos( $text, $custom_merge_tag ) === false ) {
return $text;
}
$score = array();
foreach($form['fields'] as $field) {
if( $field['type'] == "radio") {
if( isset( $score[ $entry[ $field['id'] ] ] ) ) {
$score[ $entry[ $field['id'] ] ] = $score[ $entry[ $field['id'] ] ] + 1;
} else {
$score[ $entry[ $field['id'] ] ] = 1;
}
}
}
$result = "";
foreach($score as $key => $value) {
$result .= $value . "&nbsp;&nbsp;&nbsp;&nbsp;" . $key ."<br/>";
}
$text = str_replace( $custom_merge_tag, $result, $text );
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment