I basically want to have a custom CMS page that has pairs of images and labels for those images defined within it. I intend to use these pairs of items for populating content on a custom page of my new WordPress theme.
I have managed to make a new settings page within the CMS and populate any number of text boxes within it all thanks to Handling Plugins Options in WordPress 2.8 with register_setting()
.
I just now need to add fields for each text box that let a user open up the media browser and then select an existing image uploaded to WordPress or upload a new one for selection.
I haven’t been able to find any simple clean examples of this online. Even the questions related to this on Stack Overflow I found don’t seem to have a clear elegant example of this working.
Just for the record; I initially planned to do this with a plugin (Developers Custom Fields) which I already have installed, thinking it would be very easy with this plugin to add custom image fields because it is pretty simple adding text boxes with it. However I seem to be wrong and no clear examples exist for specifying this kind of field.
If you know of any examples that illustrate adding custom image fields (irrelevant of adding other fields at the same time), either using native WordPress API code or integration with the Developers Custom Fields plugin.
You don’t exactly mention it, but it seems that you are dealing with Repeatable Fields.
Two plugins of interest: Advanced Custom Fields and Custom Content Type Manager.
Today, I’ve seen this article in one of WordPress communities at G+:
Using the WordPress 3.5 Media Uploader within plugins.
This solution was originally published here in a SO Question that has been deleted since. It is rewritten, revised and tested:
Result:
I used @brasofilo‘s answer above, however I really missed having the image ID saved as well, so I could use
wp_get_attachment_image
for example.So here’s my revised code, which also saves the image ID. I also use two arrays to define on which page templates and respectively on which post types the meta box appears. Here’s the code:
Here’s an example of how you can output the data to your theme.
For WordPress 3.5 new media uploader will open with this script change script with old one.