{"id":218133,"date":"2020-01-02T05:18:33","date_gmt":"2020-01-02T05:18:33","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=218133"},"modified":"2024-07-03T09:51:35","modified_gmt":"2024-07-03T09:51:35","slug":"how-to-use-wordpress-media-upload-in-plugin-theme","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/","title":{"rendered":"How to use WordPress Media Upload in plugin \/ theme"},"content":{"rendered":"\n<p>Using the&nbsp;WordPress media library in your own plugin\/theme will save you a bunch of time. This blog will provide an example of how to use the Media Library in a plugin\/theme environment.<\/p>\n\n\n\n<p>When using wp.media, there are a few things that we are interested. The first one is how to define the media frame.<\/p>\n\n\n\n<div class=\"wk-index-wrap\"><h3 class=\"index-title\">ENQUEUE THE SCRIPT<\/h3><\/div><div class=\"margin-bottom-50\">\n<pre class=\"EnlighterJSRAW\">&lt;?php\n  \/* Add the media upload script *\/\n  function wk_enqueue_script() {\n    \/\/Enqueue media.\n    wp_enqueue_media();\n    \/\/ Enqueue custom js file.\n    wp_register_script( &#039;wk-admin-script&#039;, plugins_url( __FILE__ ), array(&#039;jquery&#039;) );\n    wp_enqueue_script( &#039;wk-admin-script&#039; );\n  }\n  add_action(&#039;admin_enqueue_scripts&#039;, &#039;wk_enqueue_script&#039;);\n?&gt;<\/pre>\n<\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\">var wkMedia;\n\/\/ Extend the wp.media object\n    wkMedia = wp.media.frames.file_frame = wp.media({\n      title: &#039;Choose Image&#039;,\n      button: {\n      text: &#039;Choose Image&#039;\n    }, multiple: false });<\/pre>\n\n\n\n<p>This article will just show the code snippets that you need to use in order to get everything to work. The rest will be up to you to incorporate into your plugin\/theme.<\/p>\n\n\n\n<div class=\"wk-index-wrap\"><h3 class=\"index-title\">Form<\/h3><\/div><div class=\"margin-bottom-50\">\n<pre class=\"EnlighterJSRAW\">\/\/Form html\n&lt;form method=&quot;post&quot;&gt;\n  &lt;input id=&quot;wk-media-url&quot; type=&quot;text&quot; name=&quot;media&quot; \/&gt;\n  &lt;input id=&quot;wk-button&quot; type=&quot;button&quot; class=&quot;button&quot; value=&quot;Upload Image&quot; \/&gt;\n  &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; \/&gt;\n&lt;\/form&gt;<\/pre>\n<\/div>\n\n\n\n<p>We need some html tags in order to select media file and these are:-<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A text input where we will put the URL of the file we select.<\/li>\n\n\n\n<li>A button that will activate the WordPress media library.<\/li>\n\n\n\n<li>Finally a submit button so that you can do something with the URL.<\/li>\n<\/ul>\n\n\n\n<p>The second thing that we are interested in is how to capture the event when we have selected the image(s) and clicked on the Select button.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">\/\/ When a file is selected, grab the URL and set it as the text field&#039;s value\nwkMedia.on(&#039;select&#039;, function() {\n var attachment = wkMedia.state().get(&#039;selection&#039;).first().toJSON();\n $(&#039;#wk-media-url&#039;).val(attachment.url);\n});<\/pre>\n\n\n\n<div class=\"wk-index-wrap\"><h3 class=\"index-title\">JavaScript Code Example <\/h3><\/div><div class=\"margin-bottom-50\">\n<pre class=\"EnlighterJSRAW\">jQuery(document).ready(function($){\n  \/\/ Define a variable wkMedia\n  var wkMedia;\n\n  $(&#039;#wk-button&#039;).click(function(e) {\n    e.preventDefault();\n    \/\/ If the upload object has already been created, reopen the dialog\n      if (wkMedia) {\n      wkMedia.open();\n      return;\n    }\n    \/\/ Extend the wp.media object\n    wkMedia = wp.media.frames.file_frame = wp.media({\n      title: &#039;Select media&#039;,\n      button: {\n      text: &#039;Select media&#039;\n    }, multiple: false });\n\n    \/\/ When a file is selected, grab the URL and set it as the text field&#039;s value\n    wkMedia.on(&#039;select&#039;, function() {\n      var attachment = wkMedia.state().get(&#039;selection&#039;).first().toJSON();\n      $(&#039;#wk-media-url&#039;).val(attachment.url);\n    });\n    \/\/ Open the upload dialog\n    wkMedia.open();\n  });\n});<\/pre>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"panel-0\">Support<\/h3>\n\n\n\n<p>For any technical assistance\u00a0kindly <a href=\"https:\/\/webkul.uvdesk.com\/en\/customer\/create-ticket\/\" target=\"_blank\" rel=\"noreferrer noopener\">raise\u00a0a ticket<\/a>\u00a0or\u00a0reach\u00a0us by email at\u00a0support@webkul.com. Thanks for Your Time!  Have a Good Day!<\/p>\n\n\n\n<p>Also, discover various solutions to add more features and enhance your online store by visiting the&nbsp;<a href=\"https:\/\/store.webkul.com\/woocommerce-plugins.html\" target=\"_blank\" rel=\"noreferrer noopener\">WooCommerce plugins<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using the&nbsp;WordPress media library in your own plugin\/theme will save you a bunch of time. This blog will provide an example of how to use the Media Library in a plugin\/theme environment. When using wp.media, there are a few things that we are interested. The first one is how to define the media frame. This <a href=\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":209,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1258,1501],"class_list":["post-218133","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-wordpress","tag-wordpress-plugin"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use WordPress Media Upload in plugin \/ theme - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use WordPress Media Upload in plugin \/ theme - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Using the&nbsp;WordPress media library in your own plugin\/theme will save you a bunch of time. This blog will provide an example of how to use the Media Library in a plugin\/theme environment. When using wp.media, there are a few things that we are interested. The first one is how to define the media frame. This [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-02T05:18:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-03T09:51:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Mohit Verma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mohit Verma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/\"},\"author\":{\"name\":\"Mohit Verma\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/a8cfc50e5f617fb22102e7078744f684\"},\"headline\":\"How to use WordPress Media Upload in plugin \/ theme\",\"datePublished\":\"2020-01-02T05:18:33+00:00\",\"dateModified\":\"2024-07-03T09:51:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/\"},\"wordCount\":231,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"wordpress\",\"wordpress plugin\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/\",\"name\":\"How to use WordPress Media Upload in plugin \/ theme - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2020-01-02T05:18:33+00:00\",\"dateModified\":\"2024-07-03T09:51:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use WordPress Media Upload in plugin \/ theme\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/a8cfc50e5f617fb22102e7078744f684\",\"name\":\"Mohit Verma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8422227b5a202e62c533159506db4d785ddaea670abadc139d6ed65e67617e42?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8422227b5a202e62c533159506db4d785ddaea670abadc139d6ed65e67617e42?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Mohit Verma\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/mohit-verma178\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use WordPress Media Upload in plugin \/ theme - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/","og_locale":"en_US","og_type":"article","og_title":"How to use WordPress Media Upload in plugin \/ theme - Webkul Blog","og_description":"Using the&nbsp;WordPress media library in your own plugin\/theme will save you a bunch of time. This blog will provide an example of how to use the Media Library in a plugin\/theme environment. When using wp.media, there are a few things that we are interested. The first one is how to define the media frame. This [...]","og_url":"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2020-01-02T05:18:33+00:00","article_modified_time":"2024-07-03T09:51:35+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Mohit Verma","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Mohit Verma","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/"},"author":{"name":"Mohit Verma","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/a8cfc50e5f617fb22102e7078744f684"},"headline":"How to use WordPress Media Upload in plugin \/ theme","datePublished":"2020-01-02T05:18:33+00:00","dateModified":"2024-07-03T09:51:35+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/"},"wordCount":231,"commentCount":4,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["wordpress","wordpress plugin"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/","url":"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/","name":"How to use WordPress Media Upload in plugin \/ theme - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2020-01-02T05:18:33+00:00","dateModified":"2024-07-03T09:51:35+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-use-wordpress-media-upload-in-plugin-theme\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to use WordPress Media Upload in plugin \/ theme"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/a8cfc50e5f617fb22102e7078744f684","name":"Mohit Verma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8422227b5a202e62c533159506db4d785ddaea670abadc139d6ed65e67617e42?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8422227b5a202e62c533159506db4d785ddaea670abadc139d6ed65e67617e42?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Mohit Verma"},"url":"https:\/\/webkul.com\/blog\/author\/mohit-verma178\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/218133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/209"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=218133"}],"version-history":[{"count":13,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/218133\/revisions"}],"predecessor-version":[{"id":450706,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/218133\/revisions\/450706"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=218133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=218133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=218133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}