PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Display Featured Image selection based on Conditional Logic

  1. nothingless
    Member

    I'm trying to do something that I can't quite figure out in Gravity Forms. I feel like I'm nearly there, but just having trouble putting the pieces together!

    My form submits to a Custom Post Type. There is a Post Title field, Post Body field, one Custom Taxonomy dropdown box, and two fields that aren't quite standard:

    - Category Select dropdown, which is dynamically populated by a parameter in the URL (ie, /newentry/?cat=3 which automatically sets the right category as selected in the dropdown)
    - one Image Upload field, where I have the Featured Image box ticked

    What I'm trying to do, is change the way the Image Upload field is displayed. For all posts, users will NOT be uploading an image, they will instead be choosing one from a set of images, based on the category they're posting in. I'm using Featured Image as an icon or avatar of sorts for this project. There will be between 8-25 categories, and each category will have around 10-50 icons to choose from, and they will get added to/images replaced on a regular basis (which is why I dismissed creating Conditional Logic fields for each category - it'd take forever and it'd be a hassle to add new icons regularly).

    What should happen, is that they pick their category, and get taken to the form. The category is pre-selected in the dropdown based on the URL parameter (this all works perfectly), and then I want to put something in my functions.php that removes the Upload box from the Image Upload field, and replaces it with a bunch of radio buttons, each with an image displayed as the Label, and the URL to the image as the value. When a user then selects an image in the form, it passed back to the Image Upload field, instead of an uploaded image's URL? I think I should be able to do this using gform_field_content?

    Now, the images displayed as radio buttons will depend on the category (the cat parameter in the URL), I'm not entirely sure how to grab that parameter inside a function. Originally, I had just decided to make a couple of folders on my server, each named with the category ID, so if the URL is ?cat=3, then PHP simply looks on my server in /images/3/ and loops through all JPGs in it, displaying them all as radio buttons. This seemed the easiest to me, but I'm happy to do it any other way too if that'd be easier - Media Library or something, filtered on the image name perhaps. After tinkering with this for a while, I'm suspecting the Image Upload field, since it uses the Media Library, only passes the image's ID in the library to set it as Featured Image, and not an actual URL. In which case my PHP-folder-looping idea wouldn't work.

    I am trying to do this with the Image Upload field, since that has the Set As Featured Image functionality already there. But, if it's easy enough to set a Featured Image through functions.php, then I might be better off trying to replace just a normal Radio Button field instead the Image Upload field? Hope I explained this all OK - I'm open to any ideas on how to best do this! :-)

    Posted 7 years ago on Sunday June 3, 2012 | Permalink
  2. nothingless
    Member

    Bump? Any help/ideas?

    Posted 7 years ago on Tuesday June 5, 2012 | Permalink
  3. Asking the team if anyone has any ideas on this one for you.

    Posted 7 years ago on Tuesday June 5, 2012 | Permalink
  4. Hi Nothingless,

    My recommendation would be to add a custom option to your category edit form which would allow you to select attachment images which are available for that category. You can add new options to your category edit form with the "category_edit_form" hook. Here are more details:

    http://wordpress.stackexchange.com/questions/689/adding-fields-to-the-category-tag-and-custom-taxonomy-edit-screen-in-the-wordpr

    You'll want to install a plugin that provides functions for creating/updating category meta (where the attachment image IDs will be stored for each category). Here is one plugin that will do that:

    http://wordpress.org/extend/plugins/taxonomy-metadata/

    Once you have specified a set of attachment images that are available for that category, you will need to create some custom JS that updates your "Select Image" radio field to display the images available for the selected category. This would ideally be an ajax call that is triggered on the category fields change event.

    On the gform_after_submission hook, you can create a function to retrieve the attachment ID of the selected category attachment image and use the set_post_thumbnial() function to set the featured image for the created post.

    I know this is a pretty high level overview so please feel free to ask for clarification.

    Posted 7 years ago on Tuesday June 5, 2012 | Permalink
  5. Have you resolved this?

    Posted 7 years ago on Saturday June 23, 2012 | Permalink
  6. Would the above suggestion be workable for the following circumstance:
    1. User is uploading a custom post type via gravity forms on the front-end of the site
    2. User can upload their own photo, or select one by clicking on a thumbnail or radio button
    3. Photo becomes "featured image"

    I'm using Advanced Custom Fields for my fields thus far. I need guidance on where / how I would setup the stock photo options (17 or more) and how to incorporate in the form. Thank you.

    Posted 7 years ago on Wednesday October 24, 2012 | Permalink
  7. Hi there! I'm also looking for a solution to the challenge mentioned by Brittany above. I just want users to be able to select an image from the media library and have that be the featured image for their custom post type. I've got the images as radio buttons part straightened out, but how would I set up the logic to make that selection the featured image for the post?

    Posted 7 years ago on Tuesday March 5, 2013 | Permalink
  8. Do you have your form online now? Please share the link to the page where the form is located if you do.

    Be sure the images are in the media gallery already.

    If you have used HTML img tags for the label on the radio button, I would use the attachment ID as the value. Then, when that image is selected, the value will be the attachment ID of your image. You can then set the thumbnail using the WordPress function set_post_thumbnail in a gform_after_submission hook, like this: http://pastebin.com/DK7RE9es

    Posted 7 years ago on Wednesday March 6, 2013 | Permalink