Using WordPress ‘add_media_page()’ PHP function

The add_media_page() WordPress PHP function adds a submenu page to the Media main menu in your WordPress admin dashboard.

Usage

To use the add_media_page() function, you would typically include it in a function that you then attach to the ‘admin_menu’ hook.

Here’s an example:

function my_media_subpage() {
    add_media_page(
        'My Media Subpage', // Page title
        'Media Subpage', // Menu title
        'manage_options', // Capability
        'my-media-subpage', // Menu slug
        'my_media_subpage_content' // Callback function
    );
}
add_action('admin_menu', 'my_media_subpage');

function my_media_subpage_content() {
    echo 'Welcome to my media subpage!';
}

Parameters

  • $page_title (string): The text to be displayed in the title tags of the page when the menu is selected.
  • $menu_title (string): The text to be used for the menu.
  • $capability (string): The capability required for this menu to be displayed to the user.
  • $menu_slug (string): The slug name to refer to this menu by (should be unique for this menu).
  • $callback (callable, optional): The function to be called to output the content for this page. Default is ”.
  • $position (int, optional): The position in the menu order this item should appear. Default is null.

More information

See WordPress Developer Resources: add_media_page()

The add_media_page() function is part of WordPress core and is not deprecated. The source code for this function is located in wp-admin/includes/plugin.php.

Examples

Add a media submenu with basic text output

This creates a new media submenu page that outputs a simple welcome message when visited.

function my_media_subpage() {
    add_media_page('My Media Subpage', 'Media Subpage', 'manage_options', 'my-media-subpage', 'my_media_subpage_content');
}
add_action('admin_menu', 'my_media_subpage');

function my_media_subpage_content() {
    echo 'Welcome to my media subpage!';
}

Add a media submenu with custom HTML output

This example creates a new media submenu page that outputs some custom HTML when visited.

function my_media_subpage() {
    add_media_page('My Media Subpage', 'Media Subpage', 'manage_options', 'my-media-subpage', 'my_media_subpage_content');
}
add_action('admin_menu', 'my_media_subpage');

function my_media_subpage_content() {
    echo '<h1>Welcome to my media subpage!</h1><p>Enjoy your stay.</p>';
}

Add a media submenu with position

This example creates a new media submenu page at a specific position in the submenu list.

function my_media_subpage() {
    add_media_page('My Media Subpage', 'Media Subpage', 'manage_options', 'my-media-subpage', 'my_media_subpage_content', 5);
}
add_action('admin_menu', 'my_media_subpage');

function my_media_subpage_content() {
    echo 'Welcome to my media subpage!';
}

Add a media submenu with different capability

This example creates a new media submenu page that can only be viewed by administrators.

function my_media_subpage() {
    add_media_page('My Media Subpage', 'Media Subpage', 'activate_plugins', 'my-media-subpage', 'my_media_subpage_content');
}
add_action('admin_menu', 'my_media_subpage');

function my_media_subpage_content() {
    echo 'Welcome to my media subpage!';
}

Add a media submenu with a complex callback

This example creates a new media submenu page that outputs complex HTML and uses WordPress functions in its callback.

function my_media_subpage() {
    add_media_page('My Media Subpage', 'Media Subpage', 'manage_options', 'my-media-subpage', 'my_media_subpage_content');
}
add_action('admin_menu', 'my_media_subpage');

function my_media_subpage_content() {
    echo '<h1>Welcome to my media subpage!</h1>';
    if(current_user_can('manage_options')) {
        echo '<p>You are an administrator.</p>';
    } else {
        echo '<p>You are not an administrator.</p>';
    }
}

These examples should give you a good idea of the flexibility and capabilities of the add_media_page() function.