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.

I would like to attach a ZIP file to every post I write on my blog.

I'd like to find a plugin that will do these things:

1- Insert on the post editor a box like this which allows me to upload the file.

2- Automatically insert at the bottom of each post a box that allows the reader to download the file I attached.

share|improve this question
1  
Why don't you just use the WordPress file uploader? –  westondeboer Sep 29 '12 at 18:22
    
Thanks for your comment! Do you mean the Wordpress "Add Media" feature? Because when I add certain types of file (e.g.: GPX) it tells me that it can not load them for "security reasons." Moreover, it does not automatically insert at the bottom of each post a box that allows the reader to download the file I attached. –  User Sep 29 '12 at 18:26
add comment

1 Answer

up vote 0 down vote accepted

I would still use the WordPress uploader, but you'll need to modify it to make it work. So say you want gpx files (assuming this is what you mean).

When you upload something, WordPress looks at it's file extension and based on a set of allowable mime types lets the upload go through or blocks it. Those allowed mime types are fetched by the function get_allowed_mime_types which, like nearly everything in WordPress, provides a filter to modify things. upload_mimes in this case.

We can hook in and allow .gpx files. Mime types are specified in an associative array of regex_pattern => mime_type format.

So to add gpx:

<?php
add_filter('upload_mimes', 'wpse66651_allow_gpx');
/**
 * Allow the uploading of .gpx files.
 *
 * @param   array $mimes The allowed mime types in file extension => mime format
 * @return  array
 */
function wpse66651_allow_gpx($mimes)
{
    $mimes['gpx'] = 'application/xml';
    return $mimes;
}

There are quite a few file types that WordPress recognizes and allows. If you need more, add them like the above.

As far as auto adding the file links to the content. That's easy enough. Hook into the_content get the associated attachments, spit them out.

<?php
add_filter('the_content', 'wpse66651_display_files');
function wpse66651_display_files($c)
{
    global $post;

    if(!in_the_loop())
        return $c;

    $attachments = get_posts(array(
        'post_parent'    => $post->ID,
        'post_type'      => 'attachment',
        'post_mime_type' => 'application/xml',
        'nopaging'       => true,
    ));

    if(!$attachments)
        return $c;

    $list = '<ul class="gpx-list">';
    foreach($attachments as $a)
        $list .= '<li>' . wp_get_attachment_link($a->ID) . '</li>';
    $list .= '</ul>';

    return $c . $list;
}

The above in a plugin.

share|improve this answer
    
Great answer, thank you very much! –  User Sep 30 '12 at 7:40
add comment

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.