wordpressgravity-forms-plugingravityforms

Gravity Forms Redirect Confirmation Doesn't Fire gform_after_submission hook


So I have some PHP and JS i am executing on Gravity Form Submission, and I've hit a bit of a road block that for the life of me I can't find the answer in their documentation so I'm hoping someone else has had this issue before as it seems probably quite common.

I have the following PHP code (I removed a lot of it for simplicity of the issue), essentially after submission, I populate $this->categoryStats for use in my localized script. This did work when I had the form confirmation set to a message, however, I needed to send it to a thank you page and upon changing the confirmation from Message to Page it no longer populated.

I can only assume this is down to hook priority or the gform_after_submission not firing when there's a redirect.

Any insight and ideas are welcome.

<?php

GFForms::include_addon_framework();

class GFSurveyReport extends GFAddOn {
    private static $_instance = null;
    private $categoryStats = [];

    public static function get_instance() {
        if ( self::$_instance == null ) {
            self::$_instance = new GFSurveyReport();
        }

        return self::$_instance;
    }

    public function init() {
        parent::init();
        add_action( 'gform_after_submission', array( $this, 'curate_form_answers'), 10, 2 );

        // Enqueue scripts and styles
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts_and_styles' ) );
    }

    public function enqueue_scripts_and_styles() {
        wp_enqueue_script( 'gf-survey-plugin-js', plugin_dir_url( __FILE__ ) . '../js/plugin.js', array( 'jquery' ), '1.0.0', true );
        
        // Pass PHP variables to JavaScript
        wp_localize_script( 'gf-survey-plugin-js', 'categoryStats', $this->categoryStats );
    }

    public function curate_form_answers($entry, $form){
        $this->categoryStats = $categoryStats;
    }
}

Solution

  • Thanks to some insight and recommendations from commenters the issue was that the plugin instance resets on redirect so setting PHP Cookies or trying to access via conventional means wasn't working.

    Instead I ended up having to store the value in a PHP Session and then I hooked into the Gravity Forms confirmation hook to append the values to the redirect URL prior to it executing so my JS could read the values from the URL on the new page.

    add_filter( 'gform_confirmation', array( $this, 'customize_redirect_url'), 10, 4 );
    
    public function customize_redirect_url( $confirmation, $form, $entry, $ajax ) {
        // Retrieve the original redirect URL
        $redirect_url = $confirmation['redirect'];
    
        // Start or resume a session
        if (!session_id()) {
            session_start();
        }
    
        // Check if categoryStats is set in the session
        if (isset($_SESSION['categoryStats'])) {
            // Retrieve the categoryStats data from the session
            $categoryStats = $_SESSION['categoryStats'];
    
            // Append the categoryStats data to the redirect URL
            $redirect_url .= (strpos($redirect_url, '?') !== false ? '&' : '?') . 'categoryStats=' . urlencode($categoryStats);
    
            // Update the confirmation array with the modified redirect URL
            $confirmation['redirect'] = $redirect_url;
        }
    
        return $confirmation;
    }