+

Adding Meta Boxes to Attachments in WordPress

May 2, 2013

The Metadata API in WordPress is a fantastic tool for storing extra values with objects inside of the database and extending the functionality of your website. By default wordpress has metadata set up for posts, comments and users. It can easily be extended to store data for other objects as well, but this will focus on using the post_meta table to store extra values with attachments or media uploads.

The first step to this it adding a metabox to the attachment editing form. This can be accomplished using the native wordpress add_meta_box function.

<?php
function add_our_attachment_meta(){
   add_meta_box( 'custom-attachment-meta-box', 
                 'Attachment Extra Details', 
                 'our_attachment_meta_box_callback',
                 'attachment',
                 'normal',
                 'low');
}
add_action( 'admin_init', 'add_our_attachment_meta' );
?>

This line of code adds our function to the `admin_init` hook. When fired, it will register the metabox in wordpress, so when editing an attachment it knows to place our metabox there and use the callback `our_attachment_meta_box_callback` to generate the guts for that metabox.

The next step is to define the callback

In this function we will set the photo to be a front page feature using the meta_key ‘featured_photo’:


<?php 
function our_attachment_meta_box_callback(){
     global $post; 
     $value = get_post_meta($post->ID, 'featured_photo', 1);
?>
      <p>This will render inside of the meta box.</p>
    <select name="featured_photo">
     <option value="0" <?php selected( 0, $value ); ?> >No </option>
     <option value="1" <?php selected( 1, $value ); ?> >Yes </option>
    </select>
     <?php
}
?>

The result of those two functions should be something like this:

Screen Shot 2013-05-02 at 11.25.12 PM

Finally we need to save that value as with the attachment.

This would usually be done using the save_post hook, but since this is an attachment, it uses a different hook, the `edit_attachment` hook. This is fired when attachments are edited.
We’ll need to create a function to save this:


<?php 
function save_our_attachment_meta(){
     global $post; 
     if( isset( $_POST['featured_photo'] ) ){
           update_post_meta( $post->ID, 'featured_photo', $_POST['featured_photo'] );
     }
}

add_action('edit_attachment', 'save_our_attachment_meta');
?>

With that we have the value saved in the post_meta table and can execute queries of the attachments and filter for posts with meta values matching the featured_photo == 1. There a ton of possibilities for other items you can store here, the WordPress metadata API really lends itself to your creativity.

2 responses to “Adding Meta Boxes to Attachments in WordPress”

  1. Noah Mason says:

    * This would usually be done using the save_post hook, but since this is an attachment, it uses a different hook, the `edit_attachment` hook. *

    Woooow thank you, thank you, thank you. If you wrote this post for no other reason than to help 1 person not completely lose their mind, you have succeeded on this day.

« Back to Blog