Skip to content

Instantly share code, notes, and snippets.

@jetsloth
Created October 27, 2020 22:34
Show Gist options
  • Save jetsloth/9b716e053877c0368dce5e8f8938f5e8 to your computer and use it in GitHub Desktop.
Save jetsloth/9b716e053877c0368dce5e8f8938f5e8 to your computer and use it in GitHub Desktop.
Filter Gravity Wiz Populate Anything to add Image Choices from post featured image
<?php
add_filter( "gppa_input_choices", "gppa_populate_featured_image_choices", 100, 3 );
function gppa_populate_featured_image_choices( $choices, $field, $objects ) {
if ( ! property_exists($field, 'imageChoices_enableImages') || ! $field->imageChoices_enableImages ) {
return $choices;
}
$i = 0;
foreach( $choices as &$choice ) {
$post_object = $objects[$i];
$post_id = $post_object->ID;
if ( !has_post_thumbnail($post_id) ) {
continue;
}
$choice['imageChoices_image'] = get_the_post_thumbnail_url( $post_id, "small" );
$choice['imageChoices_imageID'] = get_post_thumbnail_id( $post_id );
$i++;
}
return $choices;
}
@KZeni
Copy link

KZeni commented Jan 19, 2021

I'm so glad I searched around & found this before trying to patch this in myself. This worked exactly as I wanted.

Is Gravity Wiz's Populate Anything popular enough to consider having this be officially included in Image Choices (at which point, I suppose the function name may need to change so it doesn't conflict with people that already implemented this code)? I saw in the changelog of Populate Anything that they're making patches for Jetsloth Image Choices support, and so it didn't seem unreasonable to do so in-kind and have the support natively included without needing to find & use this code snippet for the two to work together as one might want (the filter then seeing to be optimized enough to not add too much overhead to the form processing for those that don't have populate anything.) A call for Jetsloth to make, of course. 😄

@jetsloth
Copy link
Author

@KZeni glad you found it useful! We've already helped numerous customers with implementations of the gppa_input_choices filter, all slightly different. For example, some have pulled in from a meta field in a taxonomy instead of post type etc. So including this in the plugin itself might help some but not others. Providing the demo works better for us at this point.

To your other comment, yes we work closely with the Gravity Wiz guys and that recent update they made does make it easier to integrate with their new choice templates. We'll likely look to create a small additional plugin for the integration, like they've done for their Limit Choices perk https://github.com/gravitywiz/snippet-library/blob/master/gp-limit-choices/gplc-gppa-integration.php

@KZeni
Copy link

KZeni commented Jan 19, 2021

A good point, for sure. I trust your assessment of things, and matching the same methodology Gravity Wiz uses with plugins & snippets does make some sense in cases like this (wanting to avoid a bunch of unnecessary settings, having an overly-complex filter structure, etc.)

Thanks for the quick response!

@batonac
Copy link

batonac commented Mar 17, 2021

I've successfully modified this to work with an ACF image field (in this case 'front_image') as well:

add_filter( 'gppa_input_choices',  'gppa_populate_featured_image_choices', 100, 3 );

function gppa_populate_featured_image_choices( $choices, $field, $objects ) {

	if ( ! property_exists($field, 'imageChoices_enableImages') || ! $field->imageChoices_enableImages ) {
		return $choices;
	}
	
	$i = 0;
	foreach( $choices as &$choice ) {
		$post_object = $objects[$i];

		$post_id = $post_object->ID;

		$front_photo = get_field( 'front_photo', $post_id );

		if ( $front_photo ) {
			$choice['imageChoices_image'] = $front_photo['sizes']['thumbnail'];
			$choice['imageChoices_imageID'] = $front_photo['id'];
		}
		$i++;
	}

	return $choices;

}

@jetsloth
Copy link
Author

jetsloth commented Mar 17, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment