Hi
I am here to give source code for plug-in that upload image with each post and store the data in post meta.
<?php
/*
Plugin Name: artical
Version: 1.0
Plugin URI: http://rubyid10.blogspot.com
Author: Sameer
Author URI: http://rubyid10.blogspot.com
Description: Tool to add custom files to post
*/
define('ARTICAL_FOLDER', dirname(plugin_basename(__FILE__)));
define(DIRECTORY_SEPARATOR,'/');
define('ARTICAL_IMG_WEBSITE_FULLPATH',WP_CONTENT_DIR."/uploads/artical/website/");
define('ARTICAL_IMG_WEBSITE_URL', get_option('siteurl').'/wp-content/uploads/artical/website/');
define('ARTICAL_IMG_IPHONE_FULLPATH',WP_CONTENT_DIR."/uploads/artical/iphone/");
define('ARTICAL_IMG_IPHONE_URL', get_option('siteurl').'/wp-content/uploads/artical/iphone/');
define('THUMB_WIDTH',60);
define('THUMB_HEIGHT',60);
define('ARTICAL_IMG_INTER_FULLPATH',WP_CONTENT_DIR."/uploads/artical/inter/");
define('ARTICAL_IMG_INTER_URL', get_option('siteurl').'/wp-content/uploads/artical/inter/');
define('INTER_WIDTH',150);
define('INTER_HEIGHT',150);
////////////////////////////////
define('GALLERY_IMG_WEBSITE_FULLPATH',WP_CONTENT_DIR."/uploads/gallery/website/");
define('GALLERY_IMG_WEBSITE_URL', get_option('siteurl').'/wp-content/uploads/gallery/website/');
define('GALLERY_IMG_IPHONE_FULLPATH',WP_CONTENT_DIR."/uploads/gallery/iphone/");
define('GALLERY_IMG_IPHONE_URL', get_option('siteurl').'/wp-content/uploads/gallery/iphone/');
define('GALLERY_THUMB_WIDTH',60);
define('GALLERY_THUMB_HEIGHT',60);
define('GALLERY_IMG_INTER_FULLPATH',WP_CONTENT_DIR."/uploads/gallery/inter/");
define('GALLERY_IMG_INTER_URL', get_option('siteurl').'/wp-content/uploads/gallery/inter/');
define('GALLERY_INTER_WIDTH',150);
define('GALLERY_INTER_HEIGHT',150);
//////////////////////////////////////////////////////////
define('BLOG_IMG_WEBSITE_FULLPATH',WP_CONTENT_DIR."/uploads/blog/website/");
define('BLOG_IMG_WEBSITE_URL', get_option('siteurl').'/wp-content/uploads/blog/website/');
define('BLOG_IMG_IPHONE_FULLPATH',WP_CONTENT_DIR."/uploads/blog/iphone/");
define('BLOG_IMG_IPHONE_URL', get_option('siteurl').'/wp-content/uploads/blog/iphone/');
define('BLOG_THUMB_WIDTH',60);
define('BLOG_THUMB_HEIGHT',60);
define('BLOG_IMG_INTER_FULLPATH',WP_CONTENT_DIR."/uploads/blog/inter/");
define('BLOG_IMG_INTER_URL', get_option('siteurl').'/wp-content/uploads/blog/inter/');
define('BLOG_INTER_WIDTH',150);
define('BLOG_INTER_HEIGHT',150);
define('UPLOAD_IMAGE_TYPE','gif,png,jpeg,jpg');
//t
add_action('admin_menu','change_form');
/* Use the admin_menu action to define the custom boxes */
//this will add artical_gallery_blog box
add_action('admin_menu', 'add_artical_gallery_blog');
/* Use the save_post action to do something with the data entered */
add_action('save_post', 'artical_save_postdata');
function change_form()
{
ob_start("modify_buffer");
}
function modify_buffer( $buffer )
{
$buffer = str_replace( '<form name="post"', '<form name="post" enctype="multipart/form-data"',
$buffer );
return $buffer;
}
/* Adds a custom section to the "advanced" Post and Page edit screens */
///////////////////////////////////////////////////////////////////////////
function add_artical_gallery_blog()
{
if( function_exists( 'add_meta_box' ))
{
/////////////////add artical
add_meta_box( 'artical_id', __( 'Image Upload', 'myplugin_textdomain' ),
'artical_custom_box', 'post', 'advanced','high' );
add_meta_box( 'artical_id', __( 'Image Upload', 'myplugin_textdomain' ),
'artical_custom_box', 'page', 'advanced' );
}
}
///////////////////////////////////////////////////////////////////
/* Prints the inner fields for the custom post/page section */
//////////////////this for get the category image,used in xmlrpc response
function get_category_image_names( $cat_id = false, $type = false )
{
$category_image_names = get_option( 'ciii_image_names' );
if ( $cat_id !== false && $type ) {
return $category_image_names[ $cat_id ][ $type ];
}
if ( $cat_id !== false ) {
if ( ! isset( $category_image_names[ $cat_id ] ) ) return false;
return $category_image_names[ $cat_id ];
}
return $category_image_names;
}
////////////////////////////////////////////////////////////////////////////////////
function artical_custom_box()
{
// Use nonce for verification
echo '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// The actual fields for data entry
// $image_meta_data = get_post_meta($_GET['post'], '_artical',true);
if($_GET['action']=='edit'){
$keys = get_post_custom_keys(trim($_GET['post']));
if(in_array('_artical',$keys))
{
$type ="_artical";
}else if(in_array('_gallery',$keys))
{
$type ="_gallery";
}
else
{
$type ="_blog";
}
}
?>
<strong>Note:</strong>
<br/>
<span>Choose "Article" to upload image for Article. </span>
<br/>
<span>Choose "Gallery" to upload image for Gallery. </span>
<br/>
<span>Choose "Blog" to upload image for Blog. </span>
<br/>
<?php
if($_GET['action']=='edit')
{
$div_tag="<div align='center'>";
$image_meta_data = get_post_meta($_GET['post'],$type,true);
switch($type)
{
case '_artical':
$image_path = ARTICAL_IMG_INTER_URL;
break;
case '_gallery':
$image_path = GALLERY_IMG_INTER_URL;
break;
case '_blog':
$image_path = BLOG_IMG_INTER_URL;
break;
}
$div_tag.="<img src='".$image_path.$image_meta_data."'>";
$div_tag.= "</div>";
echo $div_tag;
}
echo "
<script>
jQuery(document).ready(function($){
$('#artical_image').change(function(){
$('#artical_image_edit').attr('value','1');
});
});
</script>
";
echo '<br/><div align=\'center\'><label for="Article Image">' . __("Upload Image for", 'myplugin_textdomain' ) . '</label> ';
echo "<input type='hidden' id='artical_image_edit' name='artical_image_edit' value='0'>";
?>
<select name='image_type'>
<option value="_artical"<?php if($type == '_artical')echo "selected='yes'"?>>Article
</option>
<option value="_gallery"<?php if($type == '_gallery')echo "selected='yes'"?>>Gallery
</option>
<option value="_blog"<?php if($type == '_blog')echo "selected='yes'"?>>Blog
</option>
</select>
<?php
echo '<input type="file" name="artical_image" id=\'artical_image\'/></div>';
?>
<div align="center">
<br/>
<span>File upload limit 2MB. </span>
<br/>
<span>Supported file types: gif, png, jpg . </span>
</div>
<?php
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/* When the post is saved, saves our custom data */
function artical_save_postdata( $post_id ) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
// OK, we're authenticated: we need to find and save the data
$mydata = $_POST['myplugin_new_field'];
if($_POST['artical_image_edit'] ==1)
{
$file_upload_error="";
/*echo "<pre>";
print_r($_FILES);
exit;*/
$_FILES['artical_image']['name'] = strtolower(trim($_FILES['artical_image']['name']));
if(trim($_FILES['artical_image']['name']) == '')
{
$file_upload_error=1;
wp_redirect(get_option('siteurl').'/wp-content/themes/classic/error.php?cerror='.$file_upload_error);
exit;
}
$image_extesion_array = explode(",",UPLOAD_IMAGE_TYPE);
$image_path = pathinfo($_FILES['artical_image']['name']);
$image_extension = $image_path['extension'];
//image extesion test
if(!in_array($image_extension,$image_extesion_array))
{
$file_upload_error=2;
wp_redirect(get_option('siteurl').'/wp-content/themes/classic/error.php?cerror='.$file_upload_error);
exit;
}
if($file_size >= 2000)
{
$file_upload_error=3;
wp_redirect(get_option('siteurl').'/wp-content/themes/classic/error.php?cerror='.$file_upload_error);
exit;
}
global $wpdb;
if (!empty($_POST["ID"])){
$id = $_POST["ID"];
}
if(trim($_FILES['artical_image']['name']) != '')
{
$post_id=$_POST['post_ID'];
$filename = trim($post_id.'_'.$_FILES['artical_image']['name']);
$filename = strtolower($filename);
$filename = str_replace(' ','_',$filename);
$filename = str_replace("\'",'_',$filename);
$filename = str_replace("\&",'_',$filename);
$filename = str_replace("\$",'_',$filename);
switch(trim($_REQUEST['image_type']))
{
case '_artical':
$meta_key = '_artical';
$meta_value = $filename;
$upload_path = ARTICAL_IMG_WEBSITE_FULLPATH;
$thumbnail_path = ARTICAL_IMG_IPHONE_FULLPATH;
$thumb_width = THUMB_WIDTH;
$thumb_height = THUMB_HEIGHT;
$inter_path = ARTICAL_IMG_INTER_FULLPATH;
$inter_width = INTER_WIDTH;
$inter_height = INTER_HEIGHT;
break;
case '_gallery':
$meta_key = '_gallery';
$meta_value = $filename;
$upload_path = GALLERY_IMG_WEBSITE_FULLPATH;
$thumbnail_path = GALLERY_IMG_IPHONE_FULLPATH;
$thumb_width = GALLERY_THUMB_WIDTH;
$thumb_height = GALLERY_THUMB_HEIGHT;
$inter_path = GALLERY_IMG_INTER_FULLPATH;
$inter_width = GALLERY_INTER_WIDTH;
$inter_height = GALLERY_INTER_HEIGHT;
break;
case '_blog':
$meta_key = '_blog';
$meta_value = $filename;
$upload_path = BLOG_IMG_WEBSITE_FULLPATH;
$thumbnail_path = BLOG_IMG_IPHONE_FULLPATH;
$thumb_width = BLOG_THUMB_WIDTH;
$thumb_height = BLOG_THUMB_HEIGHT;
$inter_path = BLOG_IMG_INTER_FULLPATH;
$inter_width = BLOG_INTER_WIDTH;
$inter_height = BLOG_INTER_HEIGHT;
break;
}
if($_REQUEST['action'] == 'editpost')
{
update_post_meta($post_id,$meta_key,$meta_value);
//for delete the already exists file
/*$image_meta_data = get_post_meta($post_id,$meta_key,true);
if( is_file($upload_path.$image_meta_data))
@unlink($upload_path.$image_meta_data);
@unlink($thumbnail_path.$image_meta_data);
@unlink($inter_path.$image_meta_data);*/
}else
{
add_post_meta($post_id, $meta_key, $meta_value);
}
//file upload move
move_uploaded_file($_FILES['artical_image']['tmp_name'],
$upload_path.$filename);
//for thumbnail image
include_once('thumbnail_class.php');
$image = new Resize_Image;
$image->new_width = $thumb_width ;
$image->new_height = $thumb_height;
$image->image_to_resize = $upload_path.$filename; // Full Path to the file
$image->ratio = true; // Keep Aspect Ratio?
$image->save_folder = $thumbnail_path;
$process = $image->resize();
//fot intermediate image
$image1 = new Resize_Image;
$image1->new_width = $inter_width;
$image1->new_height = $inter_height;
$image1->image_to_resize =$upload_path.$filename; // Full Path to the file
$image1->ratio = true; // Keep Aspect Ratio?
$image1->save_folder =$inter_path;
$process = $image1->resize();
}
}//for file upload checking end
////////////////////////////////////////////////////////////////////
/*echo "<pre>";
print_r($_FILES);
exit;*/
return $mydata;
}
?>