Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created September 26, 2011 20:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save billerickson/1243276 to your computer and use it in GitHub Desktop.
Save billerickson/1243276 to your computer and use it in GitHub Desktop.
Include In Rotator option in Media Uploader
<?php
/**
* Add "Include in Rotator" option to media uploader
* Limited to Home page
*
* @param $form_fields array, fields to include in attachment form
* @param $post object, attachment record in database
* @return $form_fields, modified form fields
*/
function be_attachment_field_rotator( $form_fields, $post ) {
// // Only show on front page
if( ! ( $post->post_parent == get_option( 'page_on_front' ) ) )
return $form_fields;
// Set up options
$options = array( '1' => 'Yes', '0' => 'No' );
// Get currently selected value
$selected = get_post_meta( $post->ID, 'be_rotator_include', true );
// If no selected value, default to 'No'
if( !isset( $selected ) )
$selected = '0';
// Display each option
foreach ( $options as $value => $label ) {
$checked = '';
$css_id = 'rotator-include-option-' . $value;
if ( $selected == $value ) {
$checked = " checked='checked'";
}
$html = "<div class='rotator-include-option'><input type='radio' name='attachments[$post->ID][be-rotator-include]' id='{$css_id}' value='{$value}'$checked />";
$html .= "<label for='{$css_id}'>$label</label>";
$html .= '</div>';
$out[] = $html;
}
// Construct the form field
$form_fields['be-include-rotator'] = array(
'label' => 'Include in Rotator',
'input' => 'html',
'html' => join("\n", $out),
);
// Return all form fields
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'be_attachment_field_rotator', 10, 2 );
/**
* Save value of "Include in Rotator" selection in media uploader
*
* @param $post array, the post data for database
* @param $attachment array, attachment fields from $_POST form
* @return $post array, modified post data
*/
function be_attachment_field_rotator_save( $post, $attachment ) {
if( isset( $attachment['be-rotator-include'] ) )
update_post_meta( $post['ID'], 'be_rotator_include', $attachment['be-rotator-include'] );
return $post;
}
add_filter( 'attachment_fields_to_save', 'be_attachment_field_rotator_save', 10, 2 );
@tnorthcutt
Copy link

I'm pretty sure if( !isset( $selected ) ) (line 31 right now) will always return false, since get_post_meta() "returns an empty string if the key has not been set yet" (http://codex.wordpress.org/Function_Reference/get_post_meta#Return_Value).

@tnorthcutt
Copy link

Looks like if( '' == $selected ) would work, since the only way the value of $selected should be an empty string is if postmeta for this key hasn't been set yet. Note that using empty() doesn't work, since that returns true if passed an integer 0, a float 0.0, or a string '0'.

@tnorthcutt
Copy link

I should note that I only ran into an issue when I used this code but changed the keys in the $options array. I'm honestly a little confused about why it works as posted, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment