Gravity Forms Conditional Shortcode in post

Published on

This is a hack. Only tested on Wordpress 4.7 with Gravity Forms 2.1.1.

I was hired this week to figure out how to get some advanced logic into Gravity Forms, with a results page once the form has been submitted. For some reason, Gravity Forms only supports conditional shortcodes in the message content of confirmations and notifications. It does not work on posts and pages.

Note: There is a comment in the Gravity Forms codebase that shows that they do intend to add this feature. But I needed it asap.

So, naturally I gave in and wrote all the logic in the message content. I then realised that while it worked nicely on confirmation, there are no URLs to the confirmation pages after it has been closed. So I needed a way to support the shortcodes in posts. I spent quite a while trying to enable them, using do_shortcode to no avail.

Eventually I found a solution that is hacky but it does work.

If you try to get the message from the confirmation, it shows as raw text with the shortcodes before they are parsed. I could not find a hook that worked how I needed it to. So thats not what we want. I noticed that if shortcodes work in notifications (emails) then there must be a point where they are parsed and then sent. Thankfully, after a lot of searching through the codebase, I found the gform_after_email action. This one does have the message how we want!

This code below can go in functions.php and adds the HTML produced by the shortcodes into a new post, with the title taken from the email subject.


add_action( 'gform_after_email', 'set_post_content', 10, 4 );
function set_post_content( $is_success, $to, $subject, $message ) 
 if ($is_success) {
 $post = array();
 $post['post_title'] = $subject;
 $post['post_content'] = $message;
 wp_insert_post( $post, true );
 }
}