Google help on software/programming And Windows Tricks: Wordpress Media uploader in frontend

helponsoftware

Monday 27 April 2015

Wordpress Media uploader in frontend

<label for="image_url">Photo</label>
<input type="hidden" name="image_url" id="image_url" class="regular-text">
<input type="hidden" name="attachement_id" id="image_id" class="regular-text">
<div id="img_btn"><input type="button" name="upload-btn" id="upload-btn" class="button-secondary" value="Upload Image"></div>
<?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>

<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;
var id = uploaded_image.toJSON().id;
// Let's assign the url value to the input field
$('#image_url').val(image_url);
$('#image_id').val(id);
});
});
});

</script>


USE below FUNCTION IN " functions.php"
wp_enqueue_media();

No comments:

Post a Comment