Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active February 23, 2024 02:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpmudev-sls/26edaf04eb5afed6f43e843c870dd07e to your computer and use it in GitHub Desktop.
Save wpmudev-sls/26edaf04eb5afed6f43e843c870dd07e to your computer and use it in GitHub Desktop.
[Forminator] - Submission id in response messag. This snippet allows to use the submission id in response messages. The following macro can be used in the content `{submission_id}`
<?php
/**
* Plugin Name: [Forminator] - Submission id in response message
* Plugin URI: https://premium.wpmudev.org/
* Description: This snippet allows to use the submission id in response messages. The following macro can be used in the content `{submission_id}`
* Author: Panos Lyrakis, Amit Sonkhiya @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: SLS-2683
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
if ( ! class_exists( 'WPMUDEV_Forminator_Submission_ID_In_Response' ) ) {
class WPMUDEV_Forminator_Submission_ID_In_Response {
private static $_instance = null;
private $macro = '{submission_id}';
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_Forminator_Submission_ID_In_Response();
}
return self::$_instance;
}
public function __construct() {
add_action( 'forminator_custom_form_submit_before_set_fields', array( $this, 'temporize_entry_id' ), 20, 3 );
add_filter( 'forminator_form_ajax_submit_response', array( $this, 'inject_entry_to_response' ), 20 );
add_filter( 'forminator_form_submit_response', array( $this, 'inject_entry_to_response' ), 20 );
add_action( 'wp_footer', array( $this, 'update_js_object' ), 100 );
}
/*
** Sets the entry id in a temporary $_POST key, _forminator_temp_entry_id
** By default it is not available. Setting it to the $_POST Global Var we can add it to ajax
** response on forminator_custom_form_ajax_submit_response filter
*/
public function temporize_entry_id( $entry, $form_id, $field_data_array ) {
$_POST['_forminator_temp_entry_id'] = $entry->entry_id;
$_POST['_forminator_temp_form_id'] = $form_id;
}
/*
** Inject the form id and entry id in ajax response
*/
public function inject_entry_to_response( $response ) {
if ( isset( $_POST['_forminator_temp_entry_id'] ) && is_numeric( $_POST['_forminator_temp_entry_id'] ) &&
isset( $_POST['_forminator_temp_form_id'] ) && is_numeric( $_POST['_forminator_temp_form_id'] )
) {
$entry_id = (int) $_POST['_forminator_temp_entry_id'];
$response['entry_id'] = $entry_id;
$response['form_id'] = (int) $_POST['_forminator_temp_form_id'];
$response['message'] = str_replace( $this->macro, $entry_id, $response['message'] );
}
return $response;
}
/*
** Retrieve the entry_id and form_id in the JS
*/
public function update_js_object() {
if ( wp_script_is( 'forminator-front-scripts', 'done' ) ) {
echo '<script>
(($)=>{
$(document).ready(function() {
$(document).ajaxComplete( function( event, xhr, settings ) {
let json = xhr.responseJSON;
if ( json.hasOwnProperty("success") && json.hasOwnProperty("data") && true === json.success ) {
let data = json.data;
if(data.hasOwnProperty("form_id") && data.hasOwnProperty("entry_id")) {
alert("Form Id:" + data.form_id);
console.log("Submission Id:" + data.entry_id);
}
}
});
});
})(jQuery);
</script>';
}
}
}
if ( ! function_exists( 'wpmudev_forminator_submission_id_in_response' ) ) {
function wpmudev_forminator_submission_id_in_response() {
return WPMUDEV_Forminator_Submission_ID_In_Response::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_forminator_submission_id_in_response', 10 );
}
}
@BrewsterUK
Copy link

Hi there. This is almost exactly what I've been looking for. The only issue I have is that besides displaying the submission id as part of the inline message, I also get a box telling me the form id. I don't actually need this and would like to know how to supress it. Would anyone be kind enough to offer any pointers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment