Grab hidden field label and insert into php variable within Filter

I am trying to inject a CSS class into the form output where the class pulls from the value of a hidden form field. Yes, I know that the form has a css field already but that is used for styles.

I have targeted a form with an ID of 48 and added a custom function to add the form title in the output as “form name”. That part is working well but I am trying to target a hidden field within the form and populate that into my variable: $form_class = $form[‘hiddenfieldvaluehere’];

Is this possible to do?

add_filter( 'gform_form_tag_48', 'form_tag_ldtracking', 10, 2 );
function form_tag_ldtracking( $form_tag, $form ) {
    $form_name = $form['title'];
    $form_class = $form['hiddenfieldvaluehere'];
    $form_tag = str_replace( "<form ", "<form name='{$form_name}' class='{$form_class}' ", $form_tag );
    return $form_tag;
}

Hi Dean. Does the hidden field in the form have a default value, and that is what you are trying to pull and use in the form tag when the form renders?

Also, what are you trying to do with the form with that custom class once you have it?

Yes, the hidden field default value is what im trying to pull.

Alternatively, if I could pull that post name of the page that the form appears on that would also be sufficient.

I’m trying to insert that value as a custom attribute in the out of the form code.

<form class="hiddenfieldvalue" 

I should also mention that I was able to target the hidden field and populate the default value into the form output. But it did not support the embed url variable.

 $form_post = $form['fields'][6]->defaultValue;

I had the default value of the hidden field using a merge tag so it output as:

<form  class="{embed_post:post_title}" 

Not super helpful :frowning:

For this, normally the post ID is part of the body tag, and you could do something like:

body.page-id-133 .gform_wrapper form as your selector for CSS or jQuery.

If your end goal is to ultimately add the post title as a class (I think that a post name with a space in it will foil your plans, because <form class="Contact Us" is not going to be valid) you could grab the post title from the post, in your code, like this:

add_filter( 'gform_form_tag_48', 'form_tag_ldtracking', 10, 2 );
function form_tag_ldtracking( $form_tag, $form ) {
	global $post;
	$form_class = $post->post_title;
	$form_name = $form['title'];
	$form_tag = str_replace( "<form ", "<form name='{$form_name}' class='{$form_class}' ", $form_tag );
	return $form_tag;
}

If you let us know what you need this information about the page for (i.e. “what happens next”) we may be able to help you with alternative methods. Thank you.

thanks for the help! Here is what I think I have landed on:


//==================================
//Add Form Name & Class to Special Report GForm
//==================================
add_filter( 'gform_form_tag_48', 'form_tag_ldtracking', 10, 2 );
function form_tag_ldtracking( $form_tag, $form ) {
    $form_name = $form['title'];
    $form_class = get_the_title($post->ID);
    $form_tag = str_replace( "<form ", "<form name='{$form_name}' class='{$form_class}' ", $form_tag );
    return $form_tag;
}

GOAL: Inject post name into a css class on form output so that I can target GA goals via class while using a single form for multiple pages.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.