Site logo

Archived topic

How do I create CSS only for Gravity Forms?

1 reply · Started by Rekindle on June 28, 2021

Viewing posts 1–2 of 2

Hi,

I have some custom CSS that I only need on pages that have a Gravity Form embedded in them. I don't want this CSS loading on every single page of the site.

I have this custom CSS set up as a hook under Elements, and I have it attached to gform_loaded (a hook provided by Gravity Forms). But in the display rules, I can't figure out a way to identify only those pages that have a Gravity Form in them.

Is there a simpler way to do this?

Thanks!

Hi there,

I have some custom CSS that I only need on pages that have a Gravity Form embedded in them. I don’t want this CSS loading on every single page of the site.

Try wrapping the CSS with a PHP condition.

Example:

// conditional to check whether Gravity Forms shortcode is on a page
function has_gform() {
     global $post;
     $all_content = get_the_content();
     if (strpos($all_content,'[gravityform') !== false) {
	return true;
     } else {
	return false;
     }
}

if ( has_gform() ) { //if a gform exists.
    echo '<style> your style here </style>';
}

reference - https://clicknathan.com/web-design/has_gform/

This archived topic is closed to new replies.