Add a (Gravity Forms) Shortcode to a Genesis Archive Intro

For a long time, the Genesis Framework has allowed users to set up a custom title and description, or introduction, for archives for custom post types and terms. It used to be a fairly bare bones setup, just a headline and a text box for the description. This meant that if you wanted to add anything other than text, even something as simple as an image or a link, or something more complex–like a Gravity Form–you had to know how to create the HTML markup and do it yourself.

Last year, however, that text box was changed to a WordPress editor instead, which is much more friendly for the average user. Recently, I was working on a client site and wanted to add a Gravity Form (not an affiliate link) to a custom content type (or custom post type) introduction.

My use case: I built a simple jobs board plugin and wanted to be able to add a form to the archive so that users could submit a new jobs listing–the “Submit a Job Listing” button unrolls a Gravity Form. Another use for this, though, could be adding a simple email subscription form above the archive.

I quickly ran into two issues:

  1. although Gravity Forms provides a convenient “Add Form” button above the editor, for some reason, the button wasn’t showing on my archive settings page.
  2. although I could add the shortcode for the form to the editor content by hand, the shortcode wasn’t processed, and so the form wouldn’t render.

The second issue was the easiest to solve. I just needed to add some code to my functions.php file to tell WordPress to process shortcodes in the archive introductions:

// Parse shortcodes on custom content type archives and term archives.
add_filter( 'genesis_cpt_archive_intro_text_output', 'do_shortcode', 20 );
add_filter( 'genesis_term_intro_text_output', 'do_shortcode', 20 );Code language: JavaScript (javascript)

I’ve intentionally left out author archives, because the editor hasn’t been added to the user screens, but you could certainly include those as well, if you like. This function is also handy for other plugins/features, such as galleries, which use shortcodes.

The first issue took a bit more work. It’s not as important, I suppose, because you can add a shortcode to the editor by hand, but I’m used to the button and I like it, so I wanted to find it. Took a little searching in the Gravity Forms codebase, but I offer you this filter:

add_filter( 'gform_display_add_form_button', 'prefix_add_form_button' );
/**
 * Tell Gravity Forms to add the "Add Form" button to custom content archive
 * settings and term settings pages.
 * 
 * @param $is_post_edit_page
 *
 * @return bool
 */
function prefix_add_form_button( $is_post_edit_page ) {
	if ( in_array( RG_CURRENT_PAGE, array( 'edit.php', 'term.php' ), true ) ) {
		$is_post_edit_page = true;
	}
	return $is_post_edit_page;
}
Code language: PHP (php)

And now, a button:

Add a Gravity Forms Button to Archive Editors

Here’s what it looks like on my client site:

Jobs Board Intro with Gravity Form

There you have it–even if it’s not functionality you need often, it’s nice to know that it’s easy to add shortcodes, specifically Gravity Forms, to your archive descriptions.

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.