How to select an image from Media Library in my plugin in WordPress?

Posted by Prem Tiwari | Updated on

Want to select an image from Media Library in my plugin in WordPress? You should use wp.media to use the WordPress Media Manager dialog. First, you need to enqueue the scripts:

Your HTML could be something like this (note my code use attachment ID in the plugin setting instead of image url as you did in your answer, I think it is much better. For example, using ID allows you to get different images sizes when you need them):

Add the below code in myscript.js file.

And the Ajax action to refresh the image preview:

Reference:
https://wordpress.stackexchange.com/questions/235406/how-do-i-select-an-image-from-media-library-in-my-plugin


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers