Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#18329 closed defect (bug) (worksforme)

update_gallery_tab in media.php has unexpected behavior

Reported by: sastrugisnow Owned by:
Milestone: Priority: normal
Severity: minor Version: 3.2.1
Component: Media Keywords: has-patch
Focuses: Cc:

Description (last modified by ocean90)

wp-admin/includes/media.php has a default filter called update_gallery_tabs. The function unconditionally adds $tabs['gallery'] back into $tabs if the post has image attachments. So if a user added a filter to remove the 'gallery' tab it will be added back if the post has images. You can get around this by making sure your filter runs after the default filter.

I think a saner behavior for any tab filter that does not expressly add a tab would be to check if the tab the filter is operation on is still present in $tabs.

I first wrapped the offending $tabs!['gallery'] = sprintf(!__('Gallery (%s)'), statement in a if statement but I think an initial 'if tab is not set return' makes for more readable code.

below is the code with the 'if tab is not set return' added:

function update_gallery_tab($tabs) {
	global $wpdb;

	if ( !isset($_REQUEST['post_id']) ) {
		return $tabs;
	if ( !isset($tabs['gallery']) ) {
		return $tabs;

	$post_id = intval($_REQUEST['post_id']);

	if ( $post_id )
		$attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) );

	if ( empty($attachments) ) {
		return $tabs;

	$tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
	return $tabs;
add_filter('media_upload_tabs', 'update_gallery_tab')

Attachments (1)

update_gallery_tab.diff (364 bytes) - added by sastrugisnow 7 years ago.

Download all attachments as: .zip

Change History (3)

#1 @ocean90
7 years ago

  • Description modified (diff)

Code blocks added.

#2 @nacin
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

I don't think this modification is compatible. It would seem to prevent core from adding the gallery tab in some situations.

Simply use add_filter() with a priority > 10 and your problem goes away.

Closing as worksforme.

Note: See TracTickets for help on using tickets.