Skip to content

Instantly share code, notes, and snippets.

@zogot
Last active December 16, 2015 08:18
Show Gist options
  • Save zogot/5404498 to your computer and use it in GitHub Desktop.
Save zogot/5404498 to your computer and use it in GitHub Desktop.
<?php
// call this method inside the method that shows the metabox contents
function prefix_upload_inputs() {
global $post;
wp_enqueue_media();
$scripts = "
<script type='text/javascript'>
jQuery(document).ready(function($){
var PF_Upload = {
button: '',
ready: function() {
PF_Upload.button = $('.jMediaUploader');
PF_Upload.binds();
},
binds: function() {
PF_Upload.button.click(PF_Upload.do_upload);
},
do_upload: function(e) {
e.preventDefault();
var self = $(this),
input = self.attr('id').replace('_button', ''),
send_attachment_old = wp.media.editor.send.attachment;
wp.media.editor.send.attachment = function(props, attachment) {
var chosen_size = props.size;
$('#' + input).val(attachment.sizes[chosen_size].id);
wp.media.editor.send.attachment = send_attachment_old;
};
wp.media.editor.open(self);
}
};
PF_Upload.ready();
});
</script>
";
$name = ''; // name of setting
$value = get_post_meta( $post->ID, $name, true );
$input_text = "<input type='text' name='{$name}' id='{$name}' value='{$value}' />";
$input_button = "<button name='{$name}_button' id='{$name}_button' class='button button-secondary jMediaUploader'>" . __( 'Upload' ) . '</button>';
echo $input_text . $input_button;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment