So I am trying to get the current post id when using the gravity forms gform_field_validation
filter.
class WhiteLabel
{
public function __construct()
{
// validate the sub domain
add_filter( 'gform_field_validation_12_12', [$this, 'gf_validate_site_sub_domain'], 10, 4 );
}
public function gf_validate_site_sub_domain($result, $value, $form, $field)
{
// vars
global $wpdb;
$sub_domain = $value;
// attempt to load our sub domain from the database
$site_id = (int)$wpdb->get_var("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='site_sub_domain' AND meta_value = '" . esc_sql($sub_domain) . "'");
// if is valid and site id already exists and is not equal to current post id
if ( $result['is_valid'] && ( $site_id && ($site_id !== get_the_ID() ) ) ) {
// set is valid result as false
$result['is_valid'] = false;
// display error message
$result['message'] = '<a href="https://'.$sub_domain.'domain" target="_blank">'.$sub_domain.'.domain</a> is already in use.';
}
// return result
return $result;
}
}
new WhiteLabel();
So the above code works fine and successfully validates field when form is submitted (first time only).
When the page loads the form for the first time via ajax, this condition below works for the fist time when the form is submitted...
if ( $result['is_valid'] && ( $site_id && ($site_id !== get_the_ID() ) ) )
As you can see above get_the_ID()
is working doing it's thing (first time)
The Problem
But when my form reloads via ajax after the first submission, and then when the form is submitted for a second time, get_the_id()
does not work.
I've tried using..
global $post;
$post->ID
..inside my validation function, but this only works for first submission, and on the second submission it fails, because my error message shows even though my form is on the current post id.
Any help would be hugely appreciated, I wish I could get the $entry
data because that contains my post id.
See docs /