Skip to content

Instantly share code, notes, and snippets.

@fredrikwoll
Forked from qutek/snipet.php
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredrikwoll/84b4b7707fcaa72085b3 to your computer and use it in GitHub Desktop.
Save fredrikwoll/84b4b7707fcaa72085b3 to your computer and use it in GitHub Desktop.
Add Custom Media Upload Worpress
/*************************
* Enqueue Media Uploader
*************************/
function enqueue_admin_scripts() {
if(function_exists('wp_enqueue_media')) {
wp_enqueue_media();
}
else {
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_style('thickbox');
}
}
/**********************
* Input Media Upload
***********************/
<a href="#" class="custom_media_upload">Upload</a>
<img class="custom_media_image" src="" />
<input class="custom_media_url" type="text" name="attachment_url" value="">
<input class="custom_media_id" type="text" name="attachment_id" value="">
/********************
* Javascript
********************/
jQuery(document).ready(function($){
$('.custom_media_upload').click(function(e) {
e.preventDefault();
var custom_uploader = wp.media({
title: 'Custom Title',
button: {
text: 'Custom Button Text'
},
multiple: false // Set this to true to allow multiple files to be selected
})
.on('select', function() {
var attachment = custom_uploader.state().get('selection').first().toJSON();
$('.custom_media_image').attr('src', attachment.url);
$('.custom_media_url').val(attachment.url);
$('.custom_media_id').val(attachment.id);
})
.open();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment