Merge Tag for Media Library IDs

Adds support for fetching a comma-delimited list of attachment IDs after GPML has imported the files into the Media Library. Use the {entry:gpml_ids_FIELDID} merge tag.

Code

Filename: gpml-attachment-id-merge-tag.php

<?php
/**
 * Gravity Perks // Media Library // Merge Tag for Media Library IDs
 * https://gravitywiz.com/documentation/gravity-forms-media-library/
 *
 * Adds support for fetching a comma-delimited list of attachment IDs after GPML has imported the files into the
 * Media Library. Use the {entry:gpml_ids_FIELDID} merge tag.
 */
add_filter( 'gform_merge_tag_data', function( $data, $text, $form ) {
	foreach ( $form['fields'] as $field ) {
		if ( gp_media_library()->is_applicable_field( $field ) ) {
			$key = gp_media_library()->get_file_ids_meta_key( $field->id );
			if ( isset( $data['entry'][ $key ] ) && is_array( $data['entry'][ $key ] ) ) {
				$data['entry'][ $key ] = implode( ',', $data['entry'][ $key ] );
			}
		}
	}
	return $data;
}, 10, 3 );

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.