WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 2 years ago

Last modified 2 years ago

#23028 closed enhancement (fixed)

Multiple calls to wp_enqueue_media() breaks featured image functionality

Reported by: johnbillion Owned by:
Milestone: 3.5.1 Priority: normal
Severity: normal Version: 3.5
Component: Media Keywords:
Focuses: Cc:

Description

Situation

My custom meta box provides a button which launches the new media manager so a user can upload files.

This meta box can potentially be shown on the editing screen for a post type which supports neither a featured image nor the editor. Therefore, my meta box needs to call wp_enqueue_media() otherwise it won't have the required JS or CSS to work with.

Problem

The meta box can't blindly call wp_enqueue_media() all the time because the editing screen for a post type which supports the editor or a featured image will have already called this function. Calling it a second time causes the featured image functionality to break. (A blank media item is added to the media list and choosing an image as the featured image fails.)

My meta box therefore needs to do something like this to detect if wp_enqueue_media() has already been called:

if ( !has_action( 'admin_footer', 'wp_print_media_templates' ) )
	wp_enqueue_media();

This isn't ideal because the logic in my meta box is now tied to the internal behaviour of wp_enqueue_media().

Solution

Either prevent the featured image functionality from breaking when wp_enqueue_media() is called multiple times, or add some logic to wp_enqueue_media() so it doesn't do its thing more than once.

Change History (5)

comment:1 @son9ne2 years ago

  • Cc son9ne added

I also found that if featured image is enabled, you can simply omit wp_enqueue_media();

The code in the report helps prevent this issue too

Last edited 2 years ago by son9ne (previous) (diff)

comment:2 @nacin2 years ago

Fixed in 3.5.1 I think?

comment:3 @SergeyBiryukov2 years ago

  • Keywords close reporter-feedback added

or add some logic to wp_enqueue_media() so it doesn't do its thing more than once.

Related: [23269]

comment:4 @johnbillion2 years ago

  • Keywords close reporter-feedback removed
  • Resolution set to fixed
  • Status changed from new to closed

Confirmed that this was fixed in [23269].

comment:5 @SergeyBiryukov2 years ago

  • Milestone changed from Awaiting Review to 3.5.1
Note: See TracTickets for help on using tickets.