# Attached Files List Shortcode

> Adds an [attached_files] shortcode that lists every PDF, document or other file uploaded to the current post as download links with file type and size, ideal for resources and meeting minutes.

- Type: PHP
- Runs: Run everywhere
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: shortcodes, downloads, attachments
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/attached-files-list-shortcode/

## Code

```php
add_action( 'init', function () {
    add_shortcode( 'attached_files', function ( $atts ) {
        $atts    = shortcode_atts( array( 'type' => '', 'title' => '', 'images' => 'no', 'id' => 0 ), $atts, 'attached_files' );
        $post_id = $atts['id'] ? absint( $atts['id'] ) : get_the_ID();
        if ( ! $post_id ) {
            return '';
        }

        $files = get_children( array(
            'post_parent'    => $post_id,
            'post_type'      => 'attachment',
            'post_mime_type' => $atts['type'] ? sanitize_mime_type( $atts['type'] ) : '',
            'orderby'        => 'menu_order title',
            'order'          => 'ASC',
            'numberposts'    => 100,
        ) );

        if ( 'yes' !== $atts['images'] ) {
            $files = array_filter( $files, function ( $file ) {
                return 0 !== strpos( $file->post_mime_type, 'image/' );
            } );
        }
        if ( ! $files ) {
            return '';
        }

        $html = '' !== $atts['title'] ? '<h3 class="wpide-files-title">' . esc_html( $atts['title'] ) . '</h3>' : '';
        $html .= '<ul class="wpide-attached-files">';
        foreach ( $files as $file ) {
            $path = get_attached_file( $file->ID );
            $meta = wp_get_attachment_metadata( $file->ID );
            $size = ! empty( $meta['filesize'] ) ? (int) $meta['filesize'] : ( $path && file_exists( $path ) ? filesize( $path ) : 0 );
            $html .= sprintf(
                '<li><a href="%s" download>%s</a> <small>(%s%s)</small></li>',
                esc_url( wp_get_attachment_url( $file->ID ) ),
                esc_html( get_the_title( $file ) ),
                esc_html( strtoupper( pathinfo( (string) $path, PATHINFO_EXTENSION ) ) ),
                $size ? esc_html( ', ' . size_format( $size, 1 ) ) : ''
            );
        }
        return $html . '</ul>';
    } );
} );
```

## Notes

Example [attached_files type="application/pdf" title="Downloads"]; images are skipped unless you add images="yes".

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Attached Files List Shortcode" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
