Skip to content

Instantly share code, notes, and snippets.

@littlefyr
Created August 16, 2010 15:32
Show Gist options
  • Save littlefyr/527139 to your computer and use it in GitHub Desktop.
Save littlefyr/527139 to your computer and use it in GitHub Desktop.
$highlight_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_metabox',
'title' => 'My Custom Custom Meta',
'types' => array('page'),
'template' => STYLESHEETPATH . '/custom/metabox.php'
));
function my_admin_scripts() {
wp_enqueue_style('custom_meta_css', dirname(get_stylesheet_uri()). '/custom/meta.css');
wp_register_script('meta-upload', dirname(get_stylesheet_uri()).'/js/meta-upload.js', array('jquery','media-uload','thickbox'));
wp_enqueue_script('meta-upload');
}
function my_admin_styles() {
wp_enqueue_style('thickbox');
}
if (is_admin()) {
add_action('admin_print_scripts', 'my_admin_scripts');
add_action('admin_print_styles', 'my_admin_styles');
}
//Adapted from: http://www.webmaster-source.com/2010/01/08/using-the-wordpress-uploader-in-your-plugin-or-theme/
jQuery(function($,undefined){
// we start by making local references to some global objects so this
// will compress well.
var win=window,
doc=document,
current_upload_src,
original_send_to_editor = window.send_to_editor;
$(".upload").delegate('input.library', 'click',{}, function(event) {
var that = $(this),
caption = that.attr('title');
current_upload_src = that.siblings("input.src");
$("html").addClass("Image");
tb_show(caption, 'media-upload.php?type=image&TB_iframe=true');
return false;
});
window.send_to_editor = function(html){
if (!!original_send_to_editor) {
var fileurl = $('img', html).attr("src");
current_upload_src.val(fileurl);
current_upload_src.siblings(".preview").find("img").attr('src', fileurl);
$("html").removeClass("Image");
tb_remove();
current_upload_src = null;
} else {
window.original_send_to_editor(html);
}
};
});
<label>Mug Shot</label>
<p class="upload">
<?php $metabox->the_field('mugshot'); ?>
<input class="src" type="text" size="36" name="<?php $metabox->the_name(); ?>" value="<?php $metabox->the_value(); ?>" />
<input class="library" type="button" value="Upload Image" title="Upload a mugshot" />
<span class="preview"><img src='<?php $metabox->the_value(); ?>' style="max-width:300px;max-height:300px"</span>
<span>Upload an image</span>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment