Here is how I’ve done it. No changes to the save function are needed. I’ve been trying to save data from both fields at once, thats why it didn’t worked.
<input class="upload_file" type="text" size="45" id="picture_%1$s" name="picture_%1$s" value="%6$s" />
<input class="upload_button button" type="button" value="Upload File" />
<input class="upload_file_id" type="hidden" id="picture_%1$s_id" name="items[%1$s][picture]" value="%7$s" />
The value stored in .upload_file_id
is attachment ID (%7$s
== item['picture']
).
The value displayed in .upload_file
(%6$s
) is wp_get_attachment_image_src
like so:
$img = wp_get_attachment_image_src($item['picture']);
$img = $img[0];
And then we set %6$s
to be $img
. So in database just the attachment ID is stored, and we display link using WordPress function.