Last active
May 27, 2018 13:45
-
-
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
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
<?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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wp.media will only work if you add
wp_enqueue_media();
https://codex.wordpress.org/Javascript_Reference/wp.media