I wanted to be able to set featured images for the IPG website that I’m working on pro-bono. I spent a good part of the evening trying to get things to work with special Gravity Forms hooks, but kept running into unnecessary complexity and roadblocks. As it turns out, it’s quite simple in Pods, but not if you try doing Google searches looking for the solution.

To start with, you need to capture an action, which is the Saving of a Pods form. To do this, add a filter:

add_filter('pods_api_post_save_pod_item_member', 'ipg_pods_save_function', 10, 2);

Pods provides both a pre_save and post_save option, and the _member is the name of the pod that’s being filtered. Here’s the function itself:

// Set the uploaded image as the featured image for a given member
function ipg_pods_save_function($pieces, $is_new_item, $pod_id) {

	if(isset($pieces['fields']['member_image']['value'])){

		$key = key($pieces['fields']['member_image']['value']);
		$url = pods_image_url ( $key, $size = 'null');
		$attachment_id = pods_attachment_import ( $url, $pieces['params']->id, true );
	}
}

So here’s what’s going on. The Pods field that contains the image is called member_image . If the array value exists, that means that an image has been uploaded and saved with the pod. So isset($pieces[‘fields’][‘member_image’][‘value’]) is used to determine if there’s a file uploaded that needs managing.

The only information the pod upload reports is the post ID of the image itself, which is actually contained in an array key within the value array. That’s why $key = key($pieces[‘fields’][‘member_image’][‘value’]) is used. It determines the ID of the image that was uploaded. The next line, $url = pods_image_url ( $key, $size = ‘null’) , gets the WordPress-generated URL of the uploaded image, given the post ID contained in $key. ‘null’ is passed to tell the function to return the URL for the original image, rather than one of the resized URLs.

The parent post ID (the individual member entry itself) is derived from the $pieces array passed by the filter. $pieces[‘params’]->id returns the ID of the parent member.

Now here’s where things get surprisingly good. pods_attachment_import can be used to set the featured image. If the third parameter of the function is set to true, it goes off and sets the featured image for the specified member to the image that was uploaded and assigned to $url .

Not much code required, but took forever to find the various elements and puzzle it out. That’s why there are lab notes: because I’ll need to do this again and at least now I have it documented.

Get Plugin Update News

Get Plugin Update News

Subscribe and stay up-to-date on David's development projects and plugin status.

You have Successfully Subscribed!