Add an Add Media Link to the WordPress Admin Bar

Uploading a file in WordPress takes a detour: hover Media in the admin menu and pick Add New, or open + New in the toolbar and find Media in the dropdown. This snippet puts an Add Media link with an upload icon straight on the WordPress admin bar, one click from any screen.

The link opens the upload screen, appears in wp-admin and on the front end, and only for users who are allowed to upload files.

The snippet

<?php
/**
 * Add an "Add Media" upload link to the WordPress admin bar (toolbar).
 */
add_action( 'admin_bar_menu', 'dpc_admin_bar_add_media', 71 );

function dpc_admin_bar_add_media( $wp_admin_bar ) {
	if ( ! current_user_can( 'upload_files' ) ) {
		return;
	}

	$wp_admin_bar->add_node(
		array(
			'id'    => 'dpc-add-media',
			'title' => '<span class="ab-icon dashicons-upload" aria-hidden="true"></span><span class="ab-label">' . esc_html__( 'Add Media', 'dpc' ) . '</span>',
			'href'  => admin_url( 'media-new.php' ),
			'meta'  => array(
				'title' => __( 'Upload media files', 'dpc' ),
			),
		)
	);
}

Add it with Scripts Organizer

In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor and publish. Leave Action/Hook empty.

Everywhere is what makes the link show up in the toolbar on the front end as well. If you only want it in wp-admin, choose Admin only instead; the code stays the same. current_user_can() runs inside the admin_bar_menu callback, after WordPress has loaded the user.

The same code also works unchanged in a small plugin or an mu-plugin.

How it works

  • admin_bar_menu passes the WP_Admin_Bar object, and add_node() adds a top-level item. Priority 71 places it right after the + New menu, which WordPress adds at 70.
  • admin_url( 'media-new.php' ) builds the full URL of the upload screen, so the link also works when WordPress lives in a subdirectory. The older version hard-coded /wp-admin/upload.php, which pointed at the Media Library, not the upload screen, and broke on subdirectory installs.
  • Users without the upload_files capability, such as subscribers and contributors, do not get the link.
  • ab-icon with a Dashicons class is how core toolbar items show their icon. The label is translatable and escaped; WordPress escapes the title tooltip itself.

Customise it

  • Link to the Media Library instead: use admin_url( 'upload.php' ).
  • Move it to the far right of the left group: change 71 to 999.
  • Another icon: replace dashicons-upload with any Dashicons class, such as dashicons-admin-media.
  • More shortcuts in the toolbar: Add Dropdown Pages Links to WordPress Admin Bar.

Gotchas

  • On screens narrower than 783px WordPress hides most toolbar items, including custom ones like this.
  • If you hide the admin bar on the front end, the link only appears in wp-admin. See How to hide the admin bar?

In the Scripts Organizer library Import it with one click from Scripts Organizer.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You’ll also get DPlugins product updates. No spam, and you can unsubscribe anytime.

// newsletter

Three tested WordPress snippets a week

Blocks, security, performance, Tailwind, PHP and React. Copy, paste, ship. You'll also get DPlugins product updates. No spam, and you can unsubscribe anytime.