hello i want to use wordpress media library .
i want to user see the library when click on button i made and choose a picture and i get a link of picture wich user choosed .
actualy i want to learn how we should use this library
<button type="button" class="button button-primary">choose picture</button>
i will be so happy to you guys to answer or link some docs to study them.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Take a look at wp_enqueue_media funciton. It enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs.
Then you can execute wp.media function:
var button = document.querySelector('.button');
button.addEventListener('click', function(e) {
e.preventDefault();
var frame = wp.media({
title: 'Frame title',
multiple: false
});
frame.on('select', function () {
var attachment = frame.state().get('selection').first().toJSON();
alert(attachment.url);
});
frame.open();
});
More detailed guide: https://codex.wordpress.org/Javascript_Reference/wp.media
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0