Nesting allows you to structure complex logic by having one variable reference another. Instead of repeating the same shortcode fragments across multiple variables, you build small, focused pieces and layer them together.
Why Nesting Helps #
- Keeps formulas readable and modular
- Reduces duplication when multiple variables depend on the same logic
- Makes updates easier—adjust the base variable, and every dependent variable updates automatically
How It Works #
- Create a base variable containing your core calculation or shared logic.
- Create a derived variable whose formula includes the base variable.
- Continue layering variables as needed.
Nesting Depth and Processing Order #
Natively, GravityMath (and many other WordPress shortcodes) only allow a limited number of nested shortcode levels—five layers for GravityMath—because each layer requires its own opening and closing shortcode tags.
Global Variables removes that limitation entirely. The plugin expands your deepest variable first, then processes upward layer by layer until every nested piece has been fully resolved. At each step, the plugin hands the deepest layer off to GravityMath or the relevant shortcode provider, then uses that output to resolve the next layer up, repeating this process until the entire nested expression has been evaluated.
Because of this pre‑processing, you can nest variables as deeply as your logic requires. The only restriction is to avoid circular references, such as Variable A depending on Variable B while Variable B depends on Variable A.
Treat variables as building blocks: start small, name them clearly, and combine them thoughtfully for cleaner, more maintainable logic.
Example #
Here’s a practical example using simple, realistic GravityMath expressions:
- Variable:
subtotal- Formula:
This calculates a base subtotal from two form fields.
- Formula:
- Variable:
grand_total- Formula:
This applies a 17% increase—tax, fee, or multiplier—to the previously calculatedsubtotal.
- Formula:
When grand_total runs, the plugin expands {subtotal} into its full shortcode first. Any change to the logic inside subtotal automatically flows into grand_total (and any other variable that depends on it).