Support

Account

Home Forums General Issues Field to show an Image?

Solved

Field to show an Image?

  • I would like to display a field on my post Edit Page that shows an image.

    The image is stored in a post meta field – in this case, “press_mention_information_image”.

    I already know how to set up a Text field to show that URL and allow it be editable. But I also want to show the actual image to a user int he post edit screen.

    If there is no show-image facility/add-on, is there a way I can display the img tag in HTML?

  • I created an eample filter that will do this with a URL field. It can be modified to work with other types of fields or do other things depending on your needs. https://github.com/Hube2/acf-filters-and-functions/blob/master/render-image-in-editor.php

  • Thanks.

    How do I implement this?
    I haven’t yet implemented a filter for ACF.
    I pasted your code to functions.php, minus the enclosing PHP tags.
    I don’t see any new field type added.
    Is this supposed to change how the existing URL field type operates, displaying the image?
    It doesn’t do that, it just shows the URL.

    Thanks.

  • You’ve put the code in the right place, but you’ll need to make some modifications.

    The filter does not create a new field type. It alters the behavior of an exiting field. In this case the URL field type.

    If you want to use a text field the first thing that needs to be changed is the field type. On line 11 where the filter is added change acf/render_field/type=url to acf/render_field/type=text.

    The next thing you’ll need to do is replace some of the values with the name of your field. The name of the function should be changed to the include your field name instead of "field_name" and then name of your field needs to replace the value of $field_name on line 16.

    The last thing you’ll need to do is change validation on line 36. The reason that I chose a URL field for the example is that ACF already validates these fields to make sure they are valid URLs and the only thing that I’m checking for is that they URL ends with an image type extension.

  • @John Huebner:
    Thanks for the tips.

    I think I’ve got this working.
    I changed the references to the field name, and I commented-out the extension validation.
    I now have an image showing beneath my URL textbox.

    So I guess this will now try to interpret ALL URL field types as images, is that right?
    If so, sounds like it would be useful to be able to create a separate field type and have that be the image-from-a-URL.

    Thanks.

  • It will only work on the field name you put in the if statement on line 16. If you want it to work on additional field names you’d need to test for those.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Field to show an Image?’ is closed to new replies.