Skip to content

Instantly share code, notes, and snippets.

@blogjunkie
Last active May 27, 2018 13:45
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save blogjunkie/7887686 to your computer and use it in GitHub Desktop.
Save blogjunkie/7887686 to your computer and use it in GitHub Desktop.
Integrate the WordPress Media Uploader in Theme Options. Reference this function from your add_meta_box() callback
<?php
function upload_image_metabox() {
echo '<p><strong>Upload image</strong></p>';
echo '<input type="text" id="child_logo_url" name="' . $this->get_field_name( 'welcome-image' ) . '" value="' . esc_attr( $this->get_field_value( 'welcome-image' ) ) . '" size="50" />';
echo '<input id="child_upload_logo_button" type="button" class="button" value="Upload Image" /> ';
?>
<script type="text/javascript">
// JavaScript to launch media uploader, should be enqueued in a separate file
jQuery(document).ready(function() {
jQuery('#child_upload_logo_button').click(function() {
wp.media.editor.send.attachment = function(props, attachment) {
jQuery('#child_logo_url').val(attachment.url);
}
wp.media.editor.open(this);
return false;
});
});
</script>
<?php
}
@sekhar15
Copy link

Thanks a lot....

@wmcheung
Copy link

wmcheung commented May 27, 2018

wp.media will only work if you add wp_enqueue_media();

https://codex.wordpress.org/Javascript_Reference/wp.media

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment