WordPress.org

Ready to get started?Download WordPress

Forums

Add custom drop down media metadata field (5 posts)

  1. xpat
    Member
    Posted 1 year ago #

    Sorry for the crosspost, just realised that I should be posting here.

    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 saving and retrieving a value (even just echoing it) via drop down menu. I've searched exhaustively, but everything seems to reference checkboxes or other alternativesh.

    The point of all this is to allow gallery exclusion of panorama images, while displaying the panorama images separately. 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, using default gallery not some other one like NextGen. It seems simple enough, but I'm stuck. This code works 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.

    /**
     * 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>
    </select>";
    
    	// 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);
  2. xpat
    Member
    Posted 1 year ago #

    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.")
    	);
  3. xpat
    Member
    Posted 1 year ago #

    double post

  4. OllieJones
    Member
    Posted 1 year ago #

    Careful, xpat. The 3.5 media manager is going to mess up your approach of using the 'attachment_fields_to_edit' filter.

    Sigh.

  5. OllieJones
    Member
    Posted 1 year ago #

    There's an item in a trac ticket about this.

    http://core.trac.wordpress.org/ticket/22759#comment:1

Topic Closed

This topic has been closed to new replies.

About this Topic