Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#28090 closed defect (bug) (fixed)

Multiple add media buttons all have same ID attribute

Reported by: helgatheviking Owned by: helen
Priority: normal Milestone: 4.0
Component: Media Version: 3.5
Severity: minor Keywords: has-patch commit
Cc: Focuses:

Description

If you add an editor to a metabox (using wp_editor()) and that editor has media buttons then the content's media buttons and the new media buttons have the same ID, because the media buttons link has the same ID no matter what, which is invalid markup.

The source of the problem is the media_buttons() function:

/**
 * Adds the media button to the editor
 *
 * @since 2.5.0
 *
 * @param string $editor_id
 */
function media_buttons($editor_id = 'content') {
	$post = get_post();
	if ( ! $post && ! empty( $GLOBALS['post_ID'] ) )
		$post = $GLOBALS['post_ID'];

	wp_enqueue_media( array(
		'post' => $post
	) );

	$img = '<span class="wp-media-buttons-icon"></span> ';

	echo '<a href="#" id="insert-media-button" class="button insert-media add_media" data-editor="' . esc_attr( $editor_id ) . '" title="' . esc_attr__( 'Add Media' ) . '">' . $img . __( 'Add Media' ) . '</a>';

	/**
	 * Filter the legacy (pre-3.5.0) media buttons.
	 *
	 * @since 2.5.0
	 * @deprecated 3.5.0 Use 'media_buttons' action instead.
	 *
	 * @param string $string Media buttons context. Default empty.
	 */
	$legacy_filter = apply_filters( 'media_buttons_context', '' );

	if ( $legacy_filter ) {
		// #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
		if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) )
			$legacy_filter .= '</a>';
		echo $legacy_filter;
	}
}
add_action( 'media_buttons', 'media_buttons' );

Attachments (3)

28090.diff (1.7 KB ) - added by ericlewis 12 years ago.
28090.2.diff (1.5 KB ) - added by pento 12 years ago.
28090.3.diff (1.2 KB ) - added by nacin 12 years ago.

Download all attachments as: .zip

Change History (16)

#1 @SergeyBiryukov
12 years ago

  • Component GeneralMedia

#2 @ericlewis
12 years ago

  • Version 3.93.5

@ericlewis
12 years ago

#3 @ericlewis
12 years ago

  • Keywords has-patch added

We can't get rid of an ID here, since we need a unique identifier to pass to the wp_editor() options hash for tabfocus_elements.

In attachment:28090.diff, introduce a $static variable to keep track of the ID attribute for the anchor, and set tabfocus_elements to insert-media-button-1 in edit-form-advanced.php.

#4 @ericlewis
12 years ago

  • Severity normalminor

#5 @SergeyBiryukov
12 years ago

  • Milestone Awaiting Review4.0

#6 @wonderboymusic
12 years ago

  • Owner set to wonderboymusic
  • Resolutionfixed
  • Status newclosed

In 28837:

In media_buttons(), add a static var $instance to increment the id attribute of insert-media-button-%d on each call.

Props ericlewis.
Fixes #28090.

#7 follow-up: @nacin
12 years ago

  • Resolution fixed
  • Status closedreopened

I suspect someone may be relying on the first button being insert-media-button. I have two alternative solutions:

  • Is there a reason why IDs are needed at all here for all subsequent instances?
  • Or, can we make it so the first one is insert-media-button, and all later ones receive an ID appended?

#8 in reply to: ↑ 7 @helen
12 years ago

  • Is there a reason why IDs are needed at all here for all subsequent instances?

Sounds like an ID is required for tabfocus.

  • Or, can we make it so the first one is insert-media-button, and all later ones receive an ID appended?

Agree.

@pento
12 years ago

#9 @pento
12 years ago

attachment:28090.2.diff sets the first button's id to insert-media-button, subsequent button ids to insert-media-button-1, -2, etc.

I briefly considered starting at -2, but it implied that there should be a -1 somewhere.

@nacin
12 years ago

#10 @nacin
12 years ago

  • Keywords commit added

Let's go with 28090.3.diff. Another core or lead dev can come by and commit this if it suits them.

#11 @nacin
12 years ago

  • Owner changed from wonderboymusic to helen
  • Status reopenedassigned

Fine to be committed.

This ticket was mentioned in IRC in #wordpress-dev by SergeyBiryukov. View the logs.


12 years ago

#13 @SergeyBiryukov
12 years ago

  • Resolutionfixed
  • Status assignedclosed

In 29671:

Restore 'insert-media-button' as an ID attribute for the first instance of media_buttons(). Remove IDs for subsequent instances.

props nacin.
fixes #28090.

Note: See TracTickets for help on using tickets.