Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: b52335f265
Fetching contributors…

Cannot retrieve contributors at this time

111 lines (81 sloc) 3.325 kB
<?php
/**
* @package Post_Thumbnail_DragDrop
* @version 0.6
*/
/*
Plugin Name: Drag & Drop for Post Thumbnails
Description: Adds drag & drop to the Post Thumbnail widget.
Author: Tobias Nygren
Version: 0.6
Author URI: http://tobiasnygren.se/
*/
function dragdrop_postthumbnail_css(){
$full_plugin_url = WP_PLUGIN_URL .'/'.str_replace(basename( __FILE__),"", plugin_basename(__FILE__));
//register custom post thumbnail css
wp_register_style( 'postthumbnail-dragdrop-css', $full_plugin_url .'postthumbnail_dragdrop.css');
//enqueue css
wp_enqueue_style('imgareaselect');
wp_enqueue_style('postthumbnail-dragdrop-css' );
}
function dragdrop_postthumbnail_attach_image(){
$nonce = $_POST['_ajax_nonce'];
$post_ID = intval($_POST['post_id']);
$image_ID = intval($_POST['thumbnail_id']);
if(! wp_verify_nonce($nonce, "dragdrop_postthumbnail_attach_image-$post_ID")){
die('-1');
}
if ( !current_user_can( 'edit_post', $post_ID ) ){
die( '-1' );
}
if(wp_verify_nonce($nonce, "dragdrop_postthumbnail_attach_image-$post_ID")){
//attach image to post
global $wpdb;
$sql = $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = $post_ID WHERE ID = $image_ID LIMIT 1");
$wpdb->query($sql);
echo('1');
}
}
function dragdrop_postthumbnail($content) {
global $post_ID;
$full_plugin_url = WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__));
//register custom post thumbnail js
wp_register_script( 'postthumbnail-dragdrop-js', $full_plugin_url .'postthumbnail_dragdrop.js');
//enqueue scripts
wp_enqueue_script('plupload-handlers');
wp_enqueue_script('image-edit');
wp_enqueue_script('set-post-thumbnail' );
wp_enqueue_script('postthumbnail-dragdrop-js' );
//check if thumbnail already set
$dd_thumbnail_id = intval(get_post_meta( $post_ID, '_thumbnail_id', true ));
//create ajax nonce for set thumbnail function
$ajax_nonce_set_thumbnail = wp_create_nonce( "set_post_thumbnail-" .$post_ID );
//create ajax nonce for attachment function
$ajax_nonce_set_attachment = wp_create_nonce( "dragdrop_postthumbnail_attach_image-" .$post_ID );
?>
<div id="post_thumbnail_loading" style="display:none;height: 200px;background:url(/wp-admin/images/wpspin_light.gif) 50% 50% no-repeat;"></div>
<div id="post_thumbnail_media_upload_form">
<?php media_upload_form(); ?>
</div>
<input type="hidden" id="post_thumbnail_set_post_thumbnail_ajax_nonce" value="<?php echo $ajax_nonce_set_thumbnail; ?>" />
<input type="hidden" id="post_thumbnail_attach_post_thumbnail_ajax_nonce" value="<?php echo $ajax_nonce_set_attachment; ?>" />
<div id="post_thumbnail_image_output">
<?php
//show default content if image already chosen
if(isset($_POST['thumbnail_id'])){
$dd_thumbnail_id = intval($_POST['thumbnail_id']);
}
if($dd_thumbnail_id > 0){
echo $content;
}
?>
</div>
<?php
}//end function
//add css to admin header
add_action( 'admin_init', 'dragdrop_postthumbnail_css' );
//override admin_post_thumbnail_html()
add_filter( 'admin_post_thumbnail_html', 'dragdrop_postthumbnail' );
//ajax function for attach image to post
add_action('wp_ajax_attach_image_to_post', 'dragdrop_postthumbnail_attach_image');
?>
Jump to Line
Something went wrong with that request. Please try again.