-
-
Save fredrikwoll/84b4b7707fcaa72085b3 to your computer and use it in GitHub Desktop.
Add Custom Media Upload Worpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************* | |
* 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