Add custom drop down media metadata field (1 post)

  1. xpat
    Posted 3 years ago #

    I have my code as a standalone plugin to keep it clean. So far, I've been able to get custom check box and text fields working, that is to save states and retrieve them from the saved attachment metadata, but I am completely stuck with getting the drop down to save a value and retrieve it (even just to echo the data). Makes me realise how much of a n00b I am and hopefully somebody will have mercy :) I've spent too long (about the 15 hours over the last two days banging my head on the wall, staying up till 4am reading countless posts online) trying to figure this out. Using drop down boxes doesn't seem very popular (at least I wasn't able to find many posts on this topic).

    To the point, I want to add a drop down field to the media metadata so I can select the type of panorama image that is saved in each specific attachment. This code does work insofar as to display a drop down menu when editing an attachment, but for whatever reason, I can't get the selected value from the drop down to save in the metadata for future retrieval.

    In summation, the reason for this is because I want to allow gallery exclusion of panorama images, while displaying the panorama images separately.

     * Add custom media metadata fields
    function add_image_attachment_fields_to_edit( $form_fields, $post ) {
    	$panorama_types = "<select name='attachments[{$post->ID}][panorama]' id='attachments[{$post->ID}][panorama]'>
    	<option value=''> </option>
    	<option value='nature'>Nature</option>
    	<option value='city'>City</option>
    	<option value='other'>Other</option>
    	// Add Panorama field
    	$form_fields['panorama'] = array(
    		'label' => __('Panorama'),
    		'input' => 'html',
    		'html' => $panorama_types,
    		'helps' => __("Select type of panorama if applicable.")
    	return $form_fields;
    add_filter("attachment_fields_to_edit", "add_image_attachment_fields_to_edit", null, 2);
     * Save custom media metadata fields
    function add_image_attachment_fields_to_save( $post, $attachment ) {
    	if ( isset( $attachment['panorama'] ) )
    		update_post_meta( $post['ID'], '_panorama', $attachment['panorama'] );
    	return $post;
    add_filter("attachment_fields_to_save", "add_image_attachment_fields_to_save", null , 2);

    Instead of using HTML for the drop down, is it possible to do something like this? I hacked the code together from my reading, however I can't get it to work.

    $form_fields['panorama'] = array(
    		'name' => "attachments[{$post->ID}][panorama]",
    		'id' => "attachments[{$post->ID}][panorama],
    		'label' => __('Panorama'),
    		'type' => 'select',
    		'options' => array(
    			array('name' => 'Nature', 'value' => 'nature'),
    			array('name' => 'City', 'value' => 'city'),
    			array('name' => 'Other', 'value' => 'other')
    		'helps' => __("Select type of panorama if applicable.")

Topic Closed

This topic has been closed to new replies.

About this Topic