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.com" target="_blank">'.$sub_domain.'.domain.com</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;

..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 https://docs.gravityforms.com/gform_field_validation/

  • There is no post ID when the form is being validated. You'll need to pass the ID along with the form submission. A hidden field with the {post_id} merge tag would be the simplest way. – Jacob Peattie May 29 at 0:37
  • i'm literally working on that now, but {embed_post:ID} works the first time. Have you got 2 secs chat? – joshmoto May 29 at 2:04
  • So i thought why not just gform_pre_render the hidden field every time but upon validation the value is never outputted in the array, even though in the dom the hidden field value is set to the post id that i want to validate. It confuses me why the value is not passing to the gform_field_validation field param. – joshmoto May 29 at 2:06
  • Btw rather than filtering the specific field i've rolled back filtering the whole form so I can capture all the fields and set the id as a variable this way. Even thought i'm pre rendering a defaultValue, and it clearly saves to the gf dashboard and db. It just doesn't want parse to the field validator array. Doing my nut in! – joshmoto May 29 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.