Set Uploaded Image Data by Field Value

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gpml-set-uploaded-images-data-by-field-value.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
/**
 * Gravity Perks // Media Library // Set Uploaded Image Data by Field Value
 * https://gravitywiz.com/documentation/gravity-forms-media-library/
 */
add_filter( 'gpml_media_data', function( $media, $field, $entry ) {

	// Replace "1", "2", "3" and "4" in each line below with the ID of the field that will contain value that should be used for each image property.
	$media['post_data']['post_title']                            = rgpost( 'input_1' );
	$media['post_data']['post_content']                          = rgpost( 'input_2' );
	$media['post_data']['post_excerpt']                          = rgpost( 'input_3' );
	$media['post_data']['post_meta']['_wp_attachment_image_alt'] = rgpost( 'input_4' );

	return $media;
}, 10, 3 );

Comments

  1. Cody
    Cody September 18, 2025 at 5:53 pm

    I’ve tried you snippet with my form (Form ID 1) which has a File Upload field and the Upload to Media Library Perk enabled. I then have a basic text field setup for the image caption (field ID 20). This is part of a form that is using the GF Advanced Post Creation Add-on as well. When I submit the form, the image is in the Media Library, but the caption text entered in the field is not attached to image via Title, Description, Caption or Alt Text.

    Here’s the code based off of your snippet, https://gist.github.com/codysnodgrass/87ef9df9865fe3272461a93c558d44f6

    Here’s my form, https://beta.veteransvoices.org/archive-submissions/

    Let me know if you need anything else.

    Reply
    1. Roxy Stoltz
      Roxy Stoltz Staff September 19, 2025 at 1:32 am

      Hey Cody,

      We’re going to need to take a closer look at the current configuration to see what’s going on here. Please submit a ticket via our support form so we can take a closer look at your setup and help you further with this. 🙂

      Cheers,

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.