Home wp Gravity Forms: Use PHP Function in Conditional Logic (Simple Working Trick)

Gravity Forms: Use PHP Function in Conditional Logic (Simple Working Trick)

0
Gravity Forms: Use PHP Function in Conditional Logic (Simple Working Trick)

When working with Gravity Forms, many times you want to show dynamic data after form submit.

Like:

  • Show different content based on user answer
  • Run your own PHP function
  • Display custom posts or data

But problem comes when you try to mix everything.


-> What is the issue

Gravity Forms gives conditional shortcode like this:

[gravityforms action="conditional" merge_tag="{:3:value}" condition="contains" value="E"]</span>
<span>   Some content here</span>
<span>[/gravityforms]

Looks good, but…

  • You can’t add PHP code inside it
  • You can’t use another shortcode inside it
  • So your custom function will not run

Example that fails:

[gravityforms ...]</span>
<span>   [useful-tools]</span>
<span>[/gravityforms]

-> What we actually want

You already have:

PHP function:

useful_tools_list(array( 'type' => 'documents', 'desc' => 'true' ))

Shortcode:

[useful-tools type="documents" desc="true"]

Now you just want:
👉 Show this only when condition is true


-> Simple working idea

Don’t use Gravity Forms conditional shortcode.

👉 Put condition inside your shortcode.

Yes, very simple.


-> Step 1: Update shortcode usage

Add 2 extra things:

[useful-tools type="documents" desc="true" merge_tag="{:3:value}" value="B"]

-> Step 2: Add condition inside PHP

Now edit your shortcode function.

Main logic is this:

if (($atts['merge_tag']) && (!preg_match('/\b'.$atts['value'].'\b/', $atts['merge_tag']))) {
return '';
}

What this does:

  • Takes value from form field
  • Checks if it matches
  • If not → returns nothing

-> Full working flow

  1. User submits form
  2. Gravity Forms passes field value ({:3:value})
  3. Shortcode receives it
  4. PHP checks condition
  5. If match → show data
  6. If not → nothing shows

-> Why this method is good

  • No need to write full PHP confirmation
  • Editor still usable for team
  • Clean and simple logic
  • Works with any custom function

-> Other method (not easy)

You can use:

gform_confirmation

And then run:

do_shortcode()

But problem:

  • Everything goes in PHP
  • Non-tech users can’t edit

So better avoid if working in team.


-> Small tip

Whenever:

  • Shortcode not working inside shortcode
  • PHP not allowed in editor

👉 Move logic inside your own shortcode

This trick works in many WordPress cases.


-> Final words

Gravity Forms has limits.
But with small change in shortcode, you can:

  • Add condition
  • Run PHP function
  • Show dynamic content

Simple solution, less headache.

LEAVE A REPLY

Please enter your comment!
Please enter your name here