Created
August 28, 2012 01:05
-
-
Save claudiosanches/3494029 to your computer and use it in GitHub Desktop.
Wordpress Metabox with uploader
This file contains hidden or 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 | |
// Add metabox | |
function dfw_slideshow_metabox() { | |
add_meta_box( | |
'dfw-metabox', | |
'Slideshow', | |
'dfw_slideshow_metabox_content', | |
'post', | |
'side' | |
); | |
} | |
add_action('admin_init', 'dfw_slideshow_metabox'); | |
// Add contant for metabox | |
function dfw_slideshow_metabox_content() { | |
global $post; | |
wp_nonce_field(__FILE__, 'slideshow_metabox_nonce'); | |
?> | |
<div id="dfw-ahhc-side-wrap"> | |
<p><label for="dfw_slideshow" style="margin-right:5px;"><?php _e('Exibir post no slideshow:', 'dfwtheme'); ?></label> | |
<input type="checkbox" id="dfw_slideshow" name="dfw_slideshow" value="1" <?php checked(get_post_meta($post->ID, 'dfw_slideshow', true), 1); ?> /></p> | |
<?php $slide_thumb = get_post_meta($post->ID, 'dfw_slideshow_thumb', true); ?> | |
<p><label for="dfw_slideshow_thumb"><?php _e('Imagem:', 'dfwtheme'); ?></label><br /> | |
<input id="dfw_slideshow_thumb" type="text" name="dfw_slideshow_thumb" style="width:50%;" value="<?php if (!empty($slide_thumb)) echo $slide_thumb; ?>" /> | |
<input id="dfw_slideshow_thumb_button" type="button" class="button" value="<?php _e('Fazer upload', 'dfwtheme') ?>" /> | |
</p> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
var formfield; | |
var header_clicked = false; | |
$('#dfw_slideshow_thumb_button').click(function() { | |
formfield = $('#dfw_slideshow_thumb').attr('name'); | |
tb_show('', 'media-upload.php?post_id=<?php echo $post->ID; ?>&TB_iframe=true'); | |
header_clicked = true; | |
return false; | |
}); | |
window.original_send_to_editor = window.send_to_editor; | |
window.send_to_editor = function(html) { | |
if (header_clicked) { | |
fileurl = $(html).attr('href'); | |
$('#dfw_slideshow_thumb').val(fileurl); | |
header_clicked = false; | |
tb_remove(); | |
} else { | |
window.original_send_to_editor(html); | |
} | |
} | |
}); | |
</script> | |
</div> | |
<?php | |
} | |
// Save metabox | |
function dfw_slideshow_save_metabox($post_id) { | |
if (isset($_POST['slideshow_metabox_nonce']) && !wp_verify_nonce($_POST['slideshow_metabox_nonce'], __FILE__)) | |
return; | |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) | |
return $post_id; | |
if (!current_user_can('edit_post', $post_id)) | |
return $post_id; | |
if (get_post_type($post_id) == 'post' && $_POST) { | |
// Save checkbox | |
$value_new = esc_attr($_POST['dfw_slideshow']); | |
update_post_meta($post_id, 'dfw_slideshow', $value_new); | |
// Save image | |
$value_img = esc_attr($_POST['dfw_slideshow_thumb']); | |
update_post_meta($post_id, 'dfw_slideshow_thumb', $value_img); | |
} | |
return $post_id; | |
} | |
add_action('save_post', 'dfw_slideshow_save_metabox'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment