wordpress upload my media script – Add Media Button – download free

WordPress Image upload Script 100% working

wordpress media script to add the function.php or where you used on template page just copy and past.

For WordPress Developer Search From Correct Code of Upload image Script on Google but i got that correct way of this.

 


<?php
wp_enqueue_script('jquery');
// This will enqueue the Media Uploader script
wp_enqueue_media();
?>
 <div>
 <label for="image_url">Image</label>
 <input type="text" name="image_url" id="image_url" class="regular-text">
 <input type="button" name="upload-btn" id="upload-btn" class="button-secondary" value="Upload Image">

</div>
<script type="text/javascript">
jQuery(document).ready(function($){
 $('#upload-btn').click(function(e) {
 e.preventDefault();
 var image = wp.media({ 
 title: 'Upload Image',
 // mutiple: true if you want to upload multiple files at once
 multiple: false
 }).open()
 .on('select', function(e){
 // This will return the selected image from the Media Uploader, the result is an object
 var uploaded_image = image.state().get('selection').first();
 // We convert uploaded_image to a JSON object to make accessing it easier
 // Output to the console uploaded_image
 console.log(uploaded_image);
 var image_url = uploaded_image.toJSON().url;
 // Let's assign the url value to the input field
 $('#image_url').val(image_url);
 });
 });
});
</script>


 

 

 

 

Related Posts

Advertisement

No comments.

Leave a Reply