Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is it possible to make a meta box that attaches multiple images to a post?

share|improve this question
    
There is a complete tutorial for uploading several images adding meta-boxes to a post, page or custom post type at emenia.es/plugin-subir-imagenes-campo-personalizado-wordpress It's in Spanish, but I could get it running without problems. –  user11120 Dec 14 '11 at 12:43

4 Answers 4

up vote 6 down vote accepted

That depends entirely on what you mean by "attach."

Each WordPress post can already have multiple media attachments - photos, documents, etc. You upload these using the built-in uploader and they'll all be marked as "attached" to that specific post ID.

You can refer to these later programatically elsewhere. For example, the following code will list out all attachments for a specific post (code from Snipplr):

$args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachment) {
        echo apply_filters('the_title', $attachment->post_title);
        the_attachment_link($attachment->ID, false);
    }
}

All of this functionality is accessible via the default "Add Media" button to the far right of "Upload/Insert" on the new post screen. After you add one image, you can click "Select Files" again and upload a second image. Then a third. Then a fourth. As many as you want.

Each of these images will be "attached" to the post ... even if they're not inserted into the content.

share|improve this answer
    
For some strange reason i can only get that code to output the first attachment in the template, even though the rest are attached. Any ideas? Here is the code... dl.dropbox.com/u/497583/code/wp-attactments.txt –  agileapricot Jan 12 '11 at 21:14
    
You need to use some different variables. You're using both $post and $args in at least two different contexts, which makes it very difficult to figure out exactly what it is you're trying to do at any one time. But I did test the code I posted above and it does work ... –  EAMann Jan 12 '11 at 21:30

Yes it's quite possible. See a response I got about metaboxes. Basically, you'll just want to add a hook for save_post and verify the nonce field.

function my_save_post_callback( $post_id, $post )
{
    if ( empty($_POST) || !isset($_POST['my_custom_metabox']) || !wp_verify_nonce( $_POST['my_custom_metabox'], plugin_basename( __FILE__ ) ) )
    {
        return $post->ID;
    }

    // Handle the upload here

}
add_action( 'save_post', 'my_save_post_callback', 1, 2);
share|improve this answer

Here is a complete tutorial with source files that do exactly what you want,
You can upload multi images by cloning the input field and also you can preview, delete images with ajax, add multi metaboxes to multiple/different post types and more.

http://www.deluxeblogtips.com/2010/05/howto-meta-box-wordpress.html

share|improve this answer
    
I second this. It works like a charm. –  Manny Fleurmond Jan 13 '11 at 5:05

I'd also recommend checking out http://www.wpalchemy.com . WPAlchemy is a kick ass "class" (near to a plugin) to easily add custom meta boxes to your site. I use it extensively and have been impressed by the ease of use and commitment of the developer and burgeoning community.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.