Specify minimum image dimensions when using wp.media

I want to limit an image picker to images that are at least 650×650; how can I do this? I haven’t been able to find any reliable documentation on wp.media.

Current code:

const media_buttons = document.querySelectorAll(“button.article_figure”);

// media picker
media_buttons.forEach((button) => {
let file_frame = null;

button.addEventListener(“click”, (e) => {
e.preventDefault();

// if the file frame already exists, re-open it
if (file_frame) {
file_frame.open();
return;
}

// create a new media frame
// @TODO limit to images at least 650×650
// @TODO limit to JPG files
file_frame = wp.media.frames.file_frame = wp.media({
button: {
text: lang.media_selector_button,
},
library: {
type: [“image”],
},
title: lang.media_selector_title,
});

// update values on media selection
// @TODO remvoe jQuery dependency…
file_frame.on(“select”, () => {
const attachment = file_frame.state().get(“selection”).first().toJSON();

// store the attachment ID in the associated input
document.querySelector(“input[name=” + button.dataset.field + “]”).value = attachment.id;

// set the associated img src to the attachment URL
button.querySelector(“img”).src = attachment.url;
});

// open the frame
file_frame.open();
});
});

Read more here:: Specify minimum image dimensions when using wp.media

Leave a Reply

Your email address will not be published. Required fields are marked *