Attachments for custom posts


|

Attachment.png

Attachments can be linked to any post or page, so custom posts have not any difference and any code below work for classic posts as well.

How to add file to the post ? How to get the file ? And how to change file attributes like title for attachments ? Here you will find the answers.

First, you can upload file only by form with enctype=”multipart/form-data” so you need to add this property to the form. Post edit form has special hook post_edit_form_tag and enctype can be simply add by code like

add_action('post_edit_form_tag','publications_form_enctype');
function publications_form_enctype() {
    echo ' enctype="multipart/form-data" ';
}

then you can add somewhere html like

<input type="file" name="userfile" size="80" id="userfile" />

of course you can use ajax or flash for file uploading, but anyway you will receive the $_FILES in the php code and need to handle the uploaded temporary file in the hooks

Main function for handle files in the WordPress is media_handle_upload(). you need to provide the name of the file input and post id. In the save_post hook you can place the code like

    if ( !empty($_FILES) ) {
        // Upload File button was clicked
        $id = media_handle_upload('userfile', $post_id);
        //unset($_FILES);
        if ( is_wp_error($id) ) {
            $errors['upload_error'] = $id;
            $id = false;
        }
    }

ID provided by media_handle_upload is attachment id, which is used by function for attachment management like wp_delete_attachment() or wp_get_attachment_image(). You can save this ID or use get_posts() with post_type=attachment to request linked attachments to the post.

Normally WP can handle files only with meta_type and extension of media files – images or movie. You can disable this check by

define('ALLOW_UNFILTERED_UPLOADS',true);

Another problem will be happens if you download file on server side. For this case you have to use media_handle_sideload() function. The handle functions has also some filters for disable few checks if you have special file treatment.

Additionally files in WP has few standard meta data like caption. Usual images has internal meta data or file name used as caption. But you can overwrite caption from your form field by code in the save_post hook:

    if ( !empty($_FILES) ) {
        add_filter('wp_read_image_metadata','my_save_file_description');
        // Upload File button was clicked
        $id = media_handle_upload('userfile', $post_id);
        unset($_FILES);
        if ( is_wp_error($id) ) {
            $errors['upload_error'] = $id;
            $id = false;
        }
    }

and function for the filter above

function my_save_file_description($meta) {
    if (isset($_POST['filedesc'])) {
        $meta['caption'] = utf8_encode( trim( $_POST['filedesc'] ) );
    }
    return $meta;
}

To show attachment in the template use the_attachment_link() which provide permalink for the file.

Of course WP has a lot of abilities for use files like featured image or make gallery. Take a look into Codex for the features and keep you sites usable.

feedback content * preview