Add Form ID to body class

I can add a class to the body easy enough but how can I add the form id to the body class?

// Add current Gravity Form ID body class
 add_filter( 'body_class', 'gform_id_body_class' );
 function gform_id_body_class( $classes ) {
     $formID = ???;
     $classes[] = 'gform_id_' . $formID;
     return $classes;

body_class is a WordPress core filter, so it doesn’t has access to any Gravity Forms variables.

If you explain us what’s your goal, maybe we can point you into the right direction.

I would like to style global elements on the page that are outside of the Gravity Form wrapper. If I had a class in the body based on the form ID, I could target those elements.

You could try using some standard PHP code to search the $post->post_content for the form wrapper CSS id, that contains the form id number (e.g. gform_wrapper_5 ).

I would need the class added to the body on page load though. Looks like I’ll have to go back to the drawing board with this.

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