Attach a file to a post

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.

tech