Use image and file attachments

In WordPress you can upload, store, and display a variety of file types (media). The most common file types are image, video and audio files, but other file types such as document files, spreadsheet files, and code samples (amongst others) can also be managed within WordPress.

Media can be uploaded via the Media Add New Screen or Media Library Screen in the admin area, or via quick links on the edit screen.

In order to upload media, you may need to ensure that the correct file permissions are set for the wp-content directory in your WordPress installation.

By default, above the post editing area is a link to Upload/Insert media in to the post.

Attachment to a Post

If a media file is uploaded within the edit screen, it will automatically be attached to the current post being edited. If it is uploaded via the
Media Add New Screen or Media Library Screen it will be unattached, but may become attached to a post when it is inserted into post. There also is an option on the Media Library Screen to attach unattached media items.

Note that media items are also ‘Posts’ in their own right and can be displayed as such via the WordPress Template Hierarchy. Themes can make use of this to loop over media items or create galleries.

If the parent post of an attached media item is deleted, the media item will become attached to the deleted post’s parent if it exists or if it doesn’t exist, the media item will become unattached and is open for re-attachment to another post.

Inserting in a Post

Once a media file has been uploaded, it may be inserted in to a post.

When the media file is inserted you can choose to:

  • Link directly to the media file
  • Link to the attachment post, in which case the file will be served via the WordPress Template Hierarchy
  • Not link to the media file (e.g. in the case of images, the image file will still be embedded in the page, but not linked to allow the browser to display the image file by itself)

Usage in Themes

Themes may use various template tags to display post attachments or utilize the attachment.php file to customize the display of attachments. A shortcode tag is also available to display a gallery of images attached to a post.

Further reading

Was this article helpful? How could it be improved?

First published

Last updated