Skip to content

File

A file uploader that integrates with the WordPress media library and returns an attachment ID.

File Field

The File field opens the WordPress media library for uploading or selecting any file type — PDFs, documents, audio, ZIP files, and more. Like the Image field, it stores the attachment ID.


Field Settings

SettingDescription
Field KeyUnique identifier. Lowercase letters and underscores only.
LabelDisplay label shown above the field.
DescriptionOptional helper text shown below the field.
File TypeRestrict accepted file types, e.g. application/pdf or audio/*.
InstructionsOptional instructions shown above the upload button.
Button TextCustom label for the upload button (default: Upload File).

Return Value

Returns an integer — the WordPress attachment ID. Returns an empty string if no file is selected.


Usage

php

// Get the attachment ID
$file_id = onemeta_get_meta( get_the_ID(), 'field_key' );

if ( ! empty( $file_id ) ) {
    $file_url  = wp_get_attachment_url( $file_id );
    $file_name = basename( get_attached_file( $file_id ) );
    $file_size = size_format( filesize( get_attached_file( $file_id ) ) );
    $mime_type = get_post_mime_type( $file_id );

    echo '<a href="' . esc_url( $file_url ) . '" download>';
    echo esc_html( $file_name ) . ' (' . esc_html( $file_size ) . ')';
    echo '</a>';
}

Template Example

php

<?php $brochure_id = onemeta_get_meta( get_the_ID(), 'brochure_file' ); ?>

<?php if ( ! empty( $brochure_id ) ) : ?>
    <div class="download-card">
        <a href="<?php echo esc_url( wp_get_attachment_url( $brochure_id ) ); ?>"
           class="btn btn-download"
           download>
            📄 Download Brochure
            <span class="file-size">
                (<?php echo esc_html( size_format( filesize( get_attached_file( $brochure_id ) ) ) ); ?>)
            </span>
        </a>
    </div>
<?php endif; ?>

Notes

  • Returns an attachment ID integer — use wp_get_attachment_url() to get the file URL
  • Use get_attached_file() to get the absolute server path (needed for filesize())
  • Use get_post_mime_type() to check the file type if you need to display different UI per type

On This Page