| 1 |
document.querySelector( '.media-single-image button.browse' ).addEventListener( 'click', selectImage ); |
| 2 |
|
| 3 |
function selectImage() { |
| 4 |
const optionName = this.dataset.option; |
| 5 |
|
| 6 |
const imageFrame = wp.media( { |
| 7 |
multiple: false, |
| 8 |
library: { |
| 9 |
type: 'image', |
| 10 |
}, |
| 11 |
} ); |
| 12 |
|
| 13 |
imageFrame.on( 'select', function() { |
| 14 |
const url = imageFrame.state().get( 'selection' ).first().toJSON().url; |
| 15 |
const inputSelector = '#media-single-image-' + optionName + ' input.file-path'; |
| 16 |
document.querySelector( inputSelector ).value = url; |
| 17 |
} ); |
| 18 |
|
| 19 |
imageFrame.open(); |
| 20 |
} |
| 21 |
|