-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia-uploader.js
25 lines (23 loc) · 1.04 KB
/
media-uploader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
document.addEventListener('DOMContentLoaded', function() {
const uploadButton = document.getElementById('ct_tax_media_button');
const removeButton = document.getElementById('ct_tax_media_remove');
const imageIdInput = document.getElementById('perseo-category-image-id');
const imageWrapper = document.getElementById('category-image-wrapper');
uploadButton.addEventListener('click', function() {
const mediaUploader = wp.media({
title: 'Select an image',
button: {
text: 'Use this image'
},
multiple: false
}).on('select', function() {
const attachment = mediaUploader.state().get('selection').first().toJSON();
imageIdInput.value = attachment.id;
imageWrapper.innerHTML = `<img src="${attachment.url}" style="max-width:100%; height:auto;">`;
}).open();
});
removeButton.addEventListener('click', function() {
imageIdInput.value = '';
imageWrapper.innerHTML = '';
});
});