How do I get the current page in Gravity Forms - wordpress

How do I get the current page of a multi page form in Gravity Forms? I've got two solutions that sort of work but neither fully work.
function gf_get_current_page() {
return rgpost( 'gform_source_page_number_' . $_POST['gform_submit'] ) ? rgpost( 'gform_target_page_number_' . $_POST['gform_submit'] ) : 1;
}
This solution works until someone doesn't fill out a required field before hitting the next button. Then it says the user is on page 3 even though they're still on page 2 and need to fix validation errors.
add_action( 'gform_post_paging', 'get_current_page_num_gf', 10, 3 );
function get_current_page_num_gf( $form, $source_page_number, $current_page_number ) {
echo '<script type="text/javascript">console.log(\'' . $current_page_number . '\');</script>';
}
This works well until the validation errors again. In this scenario, instead of changing the page number, it just outputs nothing.
I'd like to know what page the user is on at all times. If they miss a required question on step 2, then I'd like to know that they're still on step 2.
In the end, I'm hoping to send all this information back to Google Analytics and getting accurate information as to what step the user is on is not working well.

Sounds like you're looking for:
GFFormDisplay::get_current_page( $form_id );

For the JS approach use:
<script type="text/javascript">
jQuery(document).on('gform_post_render', function(event, form_id, current_page){
// code to trigger on form or form page render
});
</script>
https://docs.gravityforms.com/gform_post_render/

Related

Can't correctly see Custom Events in GA4's Debug View

I'm working on a WordPress site and in order to have a custom tracking, I've tried a bunch of things but always encounter issues no matter what.
So it's an eShop (WooCommerce) and to start my tracking, I just try as a first step to add the Product Name as a custom parameter in my custom events.
Firstly, I tried to create a tag in GTM which uses a custom variable that gets its value from a data layer pushed in a JS file enqueued in my child theme.
Here's how I enqueued and populated the script, at the end of a function I called "tracking_process" :
wp_register_script('data_layer', get_stylesheet_directory_uri() . '/scripts/tracking.js', array('jquery'), null, false );
// "data" contains my product name
wp_add_inline_script('data_layer', 'var data = ' . wp_json_encode($data), 'after');
And here's the action :
add_action('wp_footer', 'tracking_process');
The script correctly loads on the page.
The product name is correctly sent to the script.
The data layer is correctly pushed as I can see in the Tag Assistant my product name in the data layer tab.
The issue is...
My data layer seems to be pushed after the tag is sent to GA4, resulting in my custom parameter always being empty in the custom event, in the Debug View.
add_action('wp_enqueue_scripts', 'tracking_process', 0);
I also tried something I found on another thread, to put the script at the beginning of the queue :
(I execute this line after "wp_register_script")
array_unshift(wp_scripts()->queue, 'data_layer');
All of this without any success, my data layer is always pushed after my tag is triggered.
So I tried another approach...
I tried to send my custom event directly from the JS file.
jQuery(document).ready( function() {
gtag('event', 'test_product', {
'product_name' : data.product_name,
});
});
Half of the time, the custom event doesn't show up in the Debug View, without any apparent reason.
When it does, it seems the "page_view" automatic event doesn't trigger.
And when I don't see the custom event, "page_view" shows up instead.
Note that in Tag Assistant, I always correctly see my custom event, so I really don't get why it doesn't in the Debug View.
The issue persists even after 48h (I've seen there can be some delay).
And that's a staging site, there's no traffic on it. The live site is brand new and has low traffic as well at the moment.
The complete PHP function :
function tracking_process(){
global $product;
if( is_product() ) {
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_id() );
}
$product_name = $product->get_name();
$data['product_name'] = $product_name;
}
wp_register_script('data_layer', get_stylesheet_directory_uri() . '/scripts/tracking.js', array('jquery'), null, false );
array_unshift(wp_scripts()->queue, 'data_layer');
wp_add_inline_script('data_layer', 'var data = ' . wp_json_encode($data), 'after');
};
add_action('wp_enqueue_scripts', 'tracking_process', 0);
I searched a lot online but found nothing that fixed this issue, and even tried with ChatGPT by giving him precise details but nothing worked so far.
Also note that I already created the custom dimensions in GA4 so it recognizes my custom parameters, and that I don't get any error in the console.
Anyone having an idea of how I can fix this issue ?
Thanks in advance !

CF7 hook breaks form after submit

I'm trying to use some CF7 hooks, but they seem to break something in the workflow after the submit.
I tried for example to add the following snippet to print something in the console:
function debug_to_console($cf7) {
echo '<div display="none"><script type="text/javascript">console.log("console log message");</script></div>';
//return $cf7;
}
add_action( 'wpcf7_before_send_mail', 'debug_to_console' );
When I enable it, nothing gets printed in the console and the [response](the notification after submitting the form) stops working.
The email instead gets delivered.
Any idea?
Thanks!
Use javascript events for frontend. https://contactform7.com/dom-events/
If you want fire action before ajax call use:
$('.wpcf7-form').submit(function() {
// action
});
We can not echo the result to front end from the wordpress hooks.
Try DOM Events from Contact form 7
document.addEventListener( 'wpcf7submit', function( event ) {
var entry = event.detail.inputs.find(element => element.name == 'entry_id');
switch(event.detail.contactFormId){
case "220": case "222":
alert(event.detail);
break;
default : console.log("Error");break;
}

Contact Form 7 redirect after submission

I'm using contact form 7 and I'm trying to redirect to another page after a successful contact form submission.
I've tried using Contact Form 7 – Success Page Redirects (https://nl-be.wordpress.org/plugins/contact-form-7-success-page-redirects/) but the plugin isn't compatible with the theme and gives some errors.
Is there another way to redirect without using that plugin?
I've found this https://contactform7.com/redirecting-to-another-url-after-submissions/ too, but I'm not able to implement it. The redirection is also only necessary for one contact form on the site, not all of them.
Thank you!
J.
I've seen quite a few answers with the same responses. The major question comes when you have 10 forms and 10 different thank you pages and this solution won't work.
I have a workaround for this.
Step 1: Create a hidden field in your form and add the thank you page URL in that.
[hidden thankyouURL id:thankyouURL default:http://example.com/thank-you/ "http://example.com/thank-you/"]
Step 2: In the DOM event, get the thank you URL from the field and redirect the user.
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
var thankyouURL = document.getElementById("thankyouURL").value;
location = thankyouURL;
}, false );
</script>
That's it.
Add below code in functions.php (located in themes -> themeName Folder)
add_action( 'wp_footer', 'mycustom_wp_footer' );
function mycustom_wp_footer() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( e ) {
var str = window.location.href;
if( str.includes("flp") ){
window.location.href = "http://www.YourWebsite.com/facebook-thank-you";
} else if( str.includes("glp") ){
window.location.href = "http://www.YourWebsite.com/google-thank-you";
}
}, false );
</script>
<?php
}
I'm trying to do the same thing but yet no success. the on_sent_ok is about to be no longer recommended. Check this page DOM EVENTS on the end of the page you can find the code for a specific form.
What do you do if you have multiple forms on the same page? Using the current answers, this could be a problem (For example, IDs must be unique, or the redirect ID is on the page but the user submits a different form).
The code below attempts to fix these potential issues. This answer uses hidden form fields in CF7, but allows you to have a unique redirect URL for each form without having to edit your JS code every time you create a new form (just use a consistent name, such as "url_redirect" as shown in the code below):
Contact Form 7:
[hidden url_redirect "http://customurl.com?customvar=1"]
Javascript:
document.addEventListener( 'wpcf7mailsent', function( e ) {
var url_redirect = '';
var inputs = e.detail.inputs;
for ( var i = 0; i < inputs.length; i++ ) {
if( 'url_redirect' == inputs[i].name ) {//used for misc forms
url_redirect = inputs[i].value;//set the redirect value from current submitted form
}
}
//Check for redirect
if( url_redirect ){
location = url_redirect;
}
}, false );
****EASY SOLUTION****
The options of using the EventListener script didn't work for me, but I found a super simple solution. Just add the Wordpress plugin called "Redirection for Contact Form 7". (Look at screenshot below).
After installing the plugin, a new tab called "Redirect Settings" will appear when you go inside any of your created Contact Forms ( Look at the 2nd screenshot). Here you have the option of either setting one of your already existing custom pages of the project as redirection url, or setting an external url for the purpose. You also have other options like setting a delay in redirection etc.

jquery functions are not working properly in multiple pages form

In my wordpress website, i am using a gravity form to complete one online application process with about 10 to 12 pages of form filling. Once a user fills out all the process and make it to payment method page (which is the last before page), he sees display of details about payment method he has chosen in previous page.
In the previous page, i had 3 options to choose like credit card, check, and money order. Now selecting credit card, works fine and displays its details in the next page. But for the other two - check and money order, anything among these are selected, the next page displays a black details area. I need to display of details according to the selection.
This is mainly because of the Jquery issue in displaying content.
In this page, there seems to be code like the below to display the contents and to stop them from displaying.
in Plugins/gravity_forms/form_display.php
<div id='gform_page_{$form["id"]}_{$field["pageNumber"]}' class='gform_page{$custom_class}' {$style}>
<div class='gform_page_fields'>
<ul class='gform_fields {$form['labelPlacement']}'>";
and li like
$style = !empty($form) && !IS_ADMIN && RGFormsModel::is_field_hidden($form, $field, $field_values) ? "style='display:none;'" : "";
$field_id = IS_ADMIN || empty($form) ? "field_$id" : "field_" . $form["id"] . "_$id";
return "<li id='$field_id' class='$css_class' $style>" . self::get_field_content($field, $value, $force_frontend_label, $form == null ? 0 : $form["id"]) . "</li>";
}
when form current page came then it show that fields other fields are display :none
I need to know how these process of display:block and none works accordingly in this situation. Which file is causing these all actions?
Here
i faced one more issue
for few id it does not show fields
so then for this i coded Themes/function-application.php
add_filter('gform_pre_render_'.$form_id, 'populate_checkbox',10);
function populate_checkbox($form)
{
in this ....
my code
if($current_page==12){
require_once 'includes/application_form/payment.php';
?>
<script type="text/javascript">
alert('inside');
jQuery("#field_1_212").css("display","block");
jQuery("#field_1_129").css("display","block");
alert('after');
</script>
<?php
}
?>
here i find only first alert inside worked fine .. and then jquery, alert after are all not worked..
so i need to know which file i need to modify ??
Thanks in advance..

Custom Post Type Action Hook / Transients

This question is in regards to a plug-in I'm developing.
I'm trying to fire a function each time a custom post type called "Product" is added or edited. In particular, I need a hook that fires before the meta boxes load on the add/edit page, but that only fires on that "Product" custom post type's edit page.
The function that will fire makes an API request, and caches the response in a transient.
The reason for the action hook is because in my current code, when the transient has expired, the add/edit page is broken during the first page load. However if you refresh the page after that, it shows up as intended. I'm fairly certain this is happening because the current conditional statement that checks the transient is located inside of the function that generates the meta box. So my theory is if I can set up an action hook to check the transient before the meta box is generated, it might solve the problem.
However I've got a second theory that the problem is being caused because of the time it takes to make the API request and return the response is longer than the time it takes for the page to load. So if there is an action hook that will delay page loading until the function finishes executing it would be an ideal solution, but I don't believe such an action hook exists. I'm not even certain if such a delay is possible.
I'd really appreciate any help or alternative suggestions you guys might have. Thanks for your time guys.
Code Example:
add_action( 'edit_product', 'llc_hook_campaign_find_active' );
function llc_hook_campaign_find_active() {
if (!$t_campaign_find_active){
limelight_cart_campaign_find_active();
return false;
}
}
Since you are using an action hook, it is not waiting for your API response.
Try using a filter hook instead.
Try using wp_insert_post_data
function filter_handler( $data , $postarr ) {
//make your API call, get the response and store it in post meta or data wherever you want
$response = 'your API response';
//e.g. update_post_meta($postarr['ID'], 'meta_key', $response); OR
//$data['post_content'] = $response;
return $data;
}
add_filter( 'wp_insert_post_data', 'filter_handler', '99', 2 );
In your case, following should work -
add_filter( 'wp_insert_post_data', 'llc_hook_campaign_find_active', '99', 2 );
function llc_hook_campaign_find_active( $data , $postarr ) {
if (!$t_campaign_find_active){
limelight_cart_campaign_find_active();
return $data;
}
}
I was able to make the API request before the meta boxes loaded on the Admin Add/Edit screen by using the action filter edit_form_top. That particular action hook is fired as soon as the Add/Edit page for any post/page/custom post type is loaded. In order to narrow it down so that the function only fires on the Add/Edit screen for my "product" custom post type, I used get_current_screen() along with an if statement.
add_action('edit_form_top', 'llc_hook_campaign_find_active');
function llc_hook_campaign_find_active() {
//Fetch current screen information
$screen = get_current_screen();
//Check if post type is "product"
if($screen->post_type == "product") {
//API Request that checks for an existing transient
$t_campaign_find_active = get_transient('campaign_find_active');
if (!$t_campaign_find_active){
limelight_cart_campaign_find_active();
return false;
}
}
}
Works like a charm.

Resources