Skip to content

Instantly share code, notes, and snippets.

@scofennell
Last active August 29, 2015 14:01
Show Gist options
  • Save scofennell/ceebef5642adaecbf131 to your computer and use it in GitHub Desktop.
Save scofennell/ceebef5642adaecbf131 to your computer and use it in GitHub Desktop.
Show custom meta boxes for attachments
<?php
/**
* Draw a custom meta box for attachments
*/
function sjf_deh_show_custom_meta_box() {
// affect the current post
global $post;
// our theme meta values for attachments
$sjf_deh_meta_fields = sjf_deh_meta_fields();
// Use nonce for verification
echo '<input type="hidden" name="sjf_deh_custom_meta_box_nonce" value="'.wp_create_nonce( basename( __FILE__ ) ).'" />';
// Begin the field table and loop
echo '<table class="form-table">';
// for each meta field, output a table row containing a select menu
foreach( $sjf_deh_meta_fields as $field ) {
// get value of this field if it exists for this post
$meta = get_post_meta( $post->ID, $field['id'], true );
$id = esc_attr( $field['id'] );
$label = esc_html( $field['label'] );
// begin a table row with the label for this field
echo "
<tr>
<th><label for='$id'>$label</label></th>
<td>
";
// what kind of field is it? We have this switch in place in case we do more fields at some point
switch( $field['type'] ) {
// if our field is a select, output it thusly
case 'select':
echo "<select name='$id' id='$id'>";
// for each option, output an option tag
foreach( $field['options'] as $o ) {
echo"<option value='$o' ".selected($o, $meta).">$o</option>";
}
echo '</select>';
break;
} //end switch
echo '
</td>
</tr>
';
} // end foreach
echo '</table>'; // end table
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment