How to add image uploader to a custom widget?

Firstly, it is not recommended to use WP_PLUGIN_URL constant, instead use plugins_url() function.

Second thing, it will not work when you put your files in the theme. You must put the necessary folder and file in the plugin if have to use this constant or function.

Make sure, you have put correct path and you JS file is being loaded properly.

wp_enque_media() is used to load necessary files and settings needed for a media uploader in the WP admin area. However, you should add necessary code specific to your uploader i.e. code for uploader on your custom post page.

The link that you have shown has covered enough code to implement WP media uploader in a plugin/theme.

Checking for my-plugin-page is not necessary. It’s added just to make sure that your script loaded only on necessary page and not all the pages. That too will be applicable when you have added an admin menu. In your case, I believe, you want the uploader on your custom post type edit page, so you can check for that instead.
Or for time being, just remove that condition, add necessary code in your my-admin.js and html in one of your plugin’s file as per tutorial given in the link shared by you.

Leave a Comment