Skip to content

Instantly share code, notes, and snippets.

@RiFi2k
Created December 9, 2017 01:26
Show Gist options
  • Save RiFi2k/f0baf931ab4e1a135bf2e2ab72a11a5b to your computer and use it in GitHub Desktop.
Save RiFi2k/f0baf931ab4e1a135bf2e2ab72a11a5b to your computer and use it in GitHub Desktop.
jQuery(document).ready(function($) {
/**
* Media Uploader
*/
var media_uploader = null;
function open_media_uploader_video(img, field) {
media_uploader = wp.media({
title: 'Select or Upload Media',
button: {
text: 'Use this media'
},
multiple: false // Set to true to allow multiple files to be selected
});
media_uploader.on( 'select', function() {
// Get media attachment details from the frame state
var attachment = media_uploader.state().get('selection').first().toJSON();
// Send the attachment URL to our custom image input field.
$(img).attr('src', attachment.url).removeClass('hide').show();
// Send the attachment id to our hidden input
$(field).val( attachment.id );
});
media_uploader.open();
}
// Open Media Uploader
$('.btn-upload').click(function(){
var img = $(this).data('imagecontainer');
var field = $(this).data('field');
if (img==undefined) {
img = '#img_container';
}
if (field==undefined) {
field = '#featured_image';
}
open_media_uploader_video(img, field);
});
// Remove Photo
$('.btn-removeupload').click(function(){
var img = $(this).data('imagecontainer');
var field = $(this).data('field');
if (img==undefined) {
img = '#img_container';
}
if (field==undefined) {
field = '#featured_image';
}
$(img).hide();
$(field).val('');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment