This example shows how to add a button in WordPress admin that opens the media library. When you select an image, its URL appears in a text box and the image preview shows below.
A. Throw a JavaScript error because wp.media() is undefined
B. Upload a new image automatically without user interaction
C. Close any open media uploader popup
D. Open the WordPress media uploader popup allowing single image selection
Solution
Step 1: Understand wp.media() usage
The code creates a media frame with a title and disables multiple selection, then opens the media uploader popup.
Step 2: Analyze each option
Open the WordPress media uploader popup allowing single image selection matches the behavior. Upload a new image automatically without user interaction is wrong because it does not upload automatically. Close any open media uploader popup is incorrect as it opens, not closes. Throw a JavaScript error because wp.media() is undefined would happen only if scripts are not loaded.
Final Answer:
Open the WordPress media uploader popup allowing single image selection -> Option D
Quick Check:
wp.media() + open() = open media popup [OK]
Hint: wp.media() with open() shows media popup [OK]
Common Mistakes:
Assuming it uploads files automatically
Confusing open() with close()
Not enqueuing media scripts causing errors
4. You wrote this code to open the media uploader but it does not open:
A. The media uploader cannot be opened inside document.ready
B. The frame.open() method is incorrect
C. You forgot to call wp_enqueue_media() to load media scripts
D. jQuery is not loaded on the page
Solution
Step 1: Check media scripts loading
The media uploader requires wp_enqueue_media() to load scripts; without it, wp.media is undefined or non-functional.
Step 2: Evaluate other options
frame.open() is correct syntax. jQuery is likely loaded if using $ inside document.ready. The uploader can open inside document.ready.
Final Answer:
You forgot to call wp_enqueue_media() to load media scripts -> Option C
Quick Check:
Missing wp_enqueue_media() = no media popup [OK]
Hint: Always enqueue media scripts before using wp.media() [OK]
Common Mistakes:
Calling frame.open() incorrectly
Assuming jQuery absence causes this
Thinking document.ready blocks media popup
5. You want to let users select multiple images from the media library and then display their URLs in a list. Which code snippet correctly sets up the media frame for multiple selection?