Created
October 27, 2020 22:34
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
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!
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;
}
-- Please reply above this line --
Hello Jetsloth,
We received your email request! Someone from our team will get back to
you as soon as possible. We're Australian Sloths and work 9am-5pm, Mon
to Fri so we'll get back to you during our sloth business hours!
Be sure to check out our support documentation here
https://jetsloth.com/support/ [1] for any help or advice.
Thank you!
Links:
------
[1] https://jetsloth.com/support/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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