Using WordPress ‘media_upload_gallery()’ PHP function

The media_upload_gallery() WordPress PHP function retrieves the legacy media uploader form in an iframe.

Usage

To use the media_upload_gallery() function, simply call it in your PHP code. Here’s an example:

media_upload_gallery();

Parameters

The media_upload_gallery() function has no parameters.

More information

See WordPress Developer Resources: media_upload_gallery

Examples

Displaying Media Uploader in a Custom Admin Page

Create a custom admin page that includes the media uploader by using the media_upload_gallery() function.

function my_custom_admin_page() {
    echo '<iframe src="' . admin_url('media-upload.php?type=image&TB_iframe=true') . '" frameborder="0"></iframe>';
    media_upload_gallery();
}

Custom Meta Box with Media Uploader

Create a custom meta box for a post type that displays the media uploader using the media_upload_gallery() function.

function my_custom_meta_box() {
    add_meta_box('custom_media_uploader', 'Custom Media Uploader', 'my_custom_meta_box_callback', 'post', 'side', 'default');
}
add_action('add_meta_boxes', 'my_custom_meta_box');

function my_custom_meta_box_callback() {
    media_upload_gallery();
}

Display Media Uploader in a Theme Options Page

Add the media uploader to a theme options page using the media_upload_gallery() function.

function my_theme_options_page() {
    echo '<div class="wrap">';
    echo '<h1>Theme Options</h1>';
    echo '<iframe src="' . admin_url('media-upload.php?type=image&TB_iframe=true') . '" frameborder="0"></iframe>';
    media_upload_gallery();
    echo '</div>';
}

Custom Plugin Options Page with Media Uploader

Create a custom plugin options page that includes the media uploader using the media_upload_gallery() function.

function my_plugin_options_page() {
    echo '<div class="wrap">';
    echo '<h1>Plugin Options</h1>';
    echo '<iframe src="' . admin_url('media-upload.php?type=image&TB_iframe=true') . '" frameborder="0"></iframe>';
    media_upload_gallery();
    echo '</div>';
}

Display Media Uploader in a Custom Widget

Create a custom widget that includes the media uploader by utilizing the media_upload_gallery() function.

class My_Custom_Widget extends WP_Widget {
    function __construct() {
        parent::__construct('my_custom_widget', 'My Custom Widget', array('description' => 'A custom widget with media uploader.'));
    }

    function widget($args, $instance) {
        echo $args['before_widget'];
        media_upload_gallery();
        echo $args['after_widget'];
    }
}
add_action('widgets_init', function () {
    register_widget('My_Custom_Widget');
});