#index.php
function load_media_files() {
wp_enqueue_media();
}
add_action( 'admin_enqueue_scripts', 'load_media_files' );
public function register_assets()
{
wp_register_script('admin-script', plugins_url('<whatever>'), ['jquery'], '1.0', true);
}
add_action('init', [$this, 'register_assets']);
#index.js
jQuery(document).ready(function ($) {
console.dir(
wp.media({
title: "Select a Song",
library: { type: "audio" },
button: {
text: "Select Song",
},
multiple: false,
})
);
let spMediaUploader;
$(".sp-upload").each(function () {
let button = $(this);
button.on("click", function (e) {
e.preventDefault();
if (spMediaUploader) {
spMediaUploader.open();
return;
}
spMediaUploader = wp.media({
title: "Select a Song",
library: { type: "audio" },
button: {
text: "Select Song",
},
multiple: false,
});
spMediaUploader.on("select", function () {
attachment = spMediaUploader
.state()
.get("selection")
.first()
.toJSON();
console.log(attachment.url);
button.parent().children(".sp-track-url").val(attachment.url);
});
spMediaUploader.open();
});
});
})