Last active
May 15, 2026 12:40
-
-
Save spivurno/095e50382450b78e7751722352791a5a to your computer and use it in GitHub Desktop.
GW Formula in Field Description
This file contains hidden or 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 | |
| /** | |
| * Plugin Name: GW Formula in Field Description | |
| * Plugin URI: https://gravitywiz.com/ | |
| * Description: Displays the configured formula as a code block in the field description for all calculation fields. | |
| * Author: Gravity Wiz | |
| * Version: 0.2 | |
| * Author URI: https://gravitywiz.com | |
| */ | |
| add_filter( 'gform_pre_render', 'gw_formula_in_description', 10, 1 ); | |
| function gw_formula_in_description( $form ) { | |
| foreach ( $form['fields'] as &$field ) { | |
| $formula = gw_get_field_formula( $field ); | |
| if ( ! $formula ) { | |
| continue; | |
| } | |
| $code_block = '<pre style="background:#f4f4f4;padding:.75em 1em;border-radius:4px;overflow-x:auto;font-size:.875em;line-height:1.5;margin:0 0 .5em;"><code>' . esc_html( $formula ) . '</code></pre>'; | |
| $field->description = $code_block . $field->description; | |
| } | |
| return $form; | |
| } | |
| function gw_get_field_formula( $field ) { | |
| // GF calculated Number fields | |
| if ( ! empty( $field->calculationFormula ) ) { | |
| return $field->calculationFormula; | |
| } | |
| // GP Formula perk | |
| if ( ! empty( $field->formula ) ) { | |
| return $field->formula; | |
| } | |
| return null; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment