Display Post Featured Image

Populate post featured image into an HTML field.

Instructions

Usage

  1. Populate the value of a field with a post ID.
  2. Insert the live merge tag for that field into the content setting of an HTML field.
  3. The live merge tag should have the :featured_image modifer like so: @{Field Label:4:featured_image} (where 4 is the ID of the field).

Code

Filename: gppa-display-post-featured-image.php

<?php
/**
 * Gravity Perks // Populate Anything // Display Post Featured Image
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * Instruction Video: https://www.loom.com/share/2193a97d68294e5e82af98e548bd47cf
 *
 * Populate post featured image into an HTML field.
 *
 * Usage:
 *
 * 1. Populate the value of a field with a post ID.
 * 2. Insert the live merge tag for that field into the content setting of an HTML field.
 * 3. The live merge tag should have the `:featured_image` modifer like so: `@{Field Label:4:featured_image}` (where 4 is the ID of the field).
 */
add_filter( 'gppa_live_merge_tag_value', function( $merge_tag_match_value, $merge_tag, $form, $field_id, $entry_values ) {

	$bits      = explode( ':', str_replace( array( '{', '}' ), '', $merge_tag ) );
	$modifiers = explode( ',', array_pop( $bits ) );
	if ( ! in_array( 'featured_image', $modifiers, true ) ) {
		return $merge_tag_match_value;
	}

	$post_id = rgar( $entry_values, $field_id );
	if ( ! $post_id ) {
		return $merge_tag_match_value;
	}

	$feautured_image = get_the_post_thumbnail( $post_id );

	return $feautured_image;
}, 10, 5 );

Leave a Reply

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

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.