How can I prevent Gravity Forms content from being included in the automatically-generate excerpt?

Hi there,

I’m noticing that my Gravity Form is showing up in the automatically-generated Wordpress excerpt:

The code to render that is <?php the_excerpt(); ?>, so I think what needs to be done is to have my form not render in the context of excerpt generation. What’s the best way to accomplish this?

I’m realizing that my case is a bit out of the ordinary, because I insert the form via a filter on the_content:

add_filter('the_content', 'append_gravity_form_to_content', 10);
function append_gravity_form_to_content($content) {
  
  // ... some logic
  $content = $content . do_shortcode('[gravityform id="' . $which_form_value . '" title="false" description="false" ajax="true"]');

  return $content;
}

So one solution to my problem here would be to know if the excerpt is being generated in my the_content filter.

Alright, in the end, I solved this by checking the stack trace for the presence of the_excerpt, and so I just don’t insert the Gravity Form when I know that my the_content filter is being run in the context of generating the_excerpt. Here’s the full code for posterity:

add_filter('the_content', 'append_gravity_form_to_content', 10);
function append_gravity_form_to_content($content) {

  $backtrace = debug_backtrace();
	foreach ( $backtrace as $trace ) {
		if ( isset( $trace['function'] ) && $trace['function'] == 'get_the_excerpt' ) {
			return $content;
		}
	}
  
  // ... some logic to determine whether the form should be added
  $content = $content . do_shortcode('[gravityform id="' . $which_form_value . '" title="false" description="false" ajax="true"]');

  return $content;
}

If anyone has a better way to do this, please let me know!

Maybe you could do this instead:

add_filter('the_content', 'append_gravity_form_to_content', 10);
function append_gravity_form_to_content($content) {
   // ... some logic to determine whether the form should be added

  if ( ! doing_filter( 'the_excerpt' ) && ! empty( $which_form_value ) ) {
    $content .= do_shortcode('[gravityform id="' . (int) $which_form_value . '" title="false" description="false" ajax="true"]');
  }

  return $content;
}
1 Like

Oh, fantastic. Thanks so much!

EDIT: ahh, shoot… relying on the value of doing_filter( 'the_excerpt' ) to know whether I need to append the shortcode to $content doesn’t work for me. Specifically: I added a log statement to:

if (doing_filter( 'the_excerpt' )) {
  // ... logging statment
}

and it never printed. I have no idea how that’s possible. Perhaps the_excerpt is being called directly, without apply_filter() ?

Anyway, I’ll continue with my stracktrace solution. Thanks so much, Jake Johnson, for your input!

1 Like

Hi Jon,

The theme must be using get_the_except() and echoing the results manually, instead of using the_excerpt() to get and echo at the same time.

Try doing_filter( 'get_the_excerpt' )

1 Like

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