Using WordPress ‘media_upload_library_form()’ PHP function

The media_upload_library_form WordPress PHP function outputs the legacy media upload form for the media library.

Usage

media_upload_library_form($errors);

Parameters

  • $errors (array) – Required. An array of error messages to be displayed with the form.

More information

See WordPress Developer Resources: media_upload_library_form

Examples

Display media upload form with no errors

$errors = array();
media_upload_library_form($errors);

Display media upload form with a single error

$errors = array("File type not supported.");
media_upload_library_form($errors);

Display media upload form with multiple errors

$errors = array("File type not supported.", "File size exceeds limit.");
media_upload_library_form($errors);

Display media upload form after checking for errors

$errors = array();
$file_type = 'text/plain';

if ($file_type !== 'image/jpeg' && $file_type !== 'image/png') {
    $errors[] = "File type not supported.";
}

media_upload_library_form($errors);

Display media upload form after validating file size

$errors = array();
$file_size = 2048000; // 2MB
$max_size = 1048576; // 1MB

if ($file_size > $max_size) {
    $errors[] = "File size exceeds limit.";
}

media_upload_library_form($errors);