Home › Forums › General Issues › 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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.