1
|
1 |
<?php |
|
2 |
|
|
3 |
// Custom fields for WP write panel |
|
4 |
// This code is protected under Creative Commons License: http://creativecommons.org/licenses/by-nc-nd/3.0/ |
|
5 |
|
|
6 |
$obox_meta = array( |
|
7 |
"thumbnail" => array ( |
|
8 |
"name" => "video_thumbnail", |
|
9 |
"default" => "", |
|
10 |
"label" => "Video Thumbnail url", |
|
11 |
"desc" => "Add an image for use as a thumbnail for your videos in the Four-Block Widget and Archives", |
|
12 |
"input_type" => "image", |
|
13 |
"input_size" => "50", |
|
14 |
"img_width" => "300", |
|
15 |
"img_height" => "300" |
|
16 |
), |
|
17 |
"media" => array ( |
|
18 |
"name" => "other_media", |
|
19 |
"default" => "", |
|
20 |
"label" => "Main Image", |
|
21 |
"desc" => "Select a header image to use for your post.", |
|
22 |
"input_type" => "textarea", |
|
23 |
"input_type" => "image", |
|
24 |
"input_size" => "50", |
|
25 |
"img_width" => "560", |
|
26 |
"img_height" => "" |
|
27 |
), |
|
28 |
"video" => array ( |
|
29 |
"name" => "main_video", |
|
30 |
"default" => "", |
|
31 |
"label" => "Video Object", |
|
32 |
"desc" => "Upload your video with the <img src='images/media-button-video.gif' alt='Add Video' /> button above the WYSIWYG, and paste url here.", |
|
33 |
"input_type" => "textarea" |
|
34 |
) |
|
35 |
); |
|
36 |
|
|
37 |
function create_meta_box_ui() { |
|
38 |
global $post, $obox_meta; |
|
39 |
$meta_count = 0; |
|
40 |
?> |
|
41 |
<table style="width: 100%;"> |
|
42 |
<?php |
|
43 |
foreach ($obox_meta as $metabox) : |
|
44 |
$meta_count = ($meta_count + 1); |
|
45 |
$obox_metabox_value = get_post_meta($post->ID,$metabox["name"],true); |
|
46 |
|
|
47 |
if ($obox_metabox_value == "" || !isset($obox_metabox_value)) : |
|
48 |
$obox_metabox_value = $metabox['default']; |
|
49 |
endif; ?> |
|
50 |
<tr> |
|
51 |
<td width="10%" valign="top"><label for="<?php echo $metabox; ?>"><?php echo $metabox["label"]; ?></label></td> |
|
52 |
<td> |
|
53 |
<?php if($metabox["name"] == "main_video") : ?> |
|
54 |
<textarea style="width: 70%;" rows="8" name="<?php echo "obox_".$metabox["name"]; ?>" id="'.$metabox.'"><?php echo $obox_metabox_value; ?></textarea> |
|
55 |
<?php elseif($metabox["input_type"] == "image") : ?> |
|
56 |
<div style="float: left;"> |
|
57 |
<input type="text" name="<?php echo "obox_".$metabox["name"]; ?>" id="<?php echo $metabox ?>" value="<?php echo $obox_metabox_value; ?>" size="<?php echo $metabox["input_size"] ?>" /> |
|
58 |
<br /><br /> |
|
59 |
<input type="file" name="<?php echo "obox_".$metabox["name"]."_file"; ?>" /> |
|
60 |
</div> |
|
61 |
<div style="float: left; width: 450px; height: 200px; display:block; overflow: hidden;"> |
|
62 |
<img src="<?php echo $obox_metabox_value; ?>" /> |
|
63 |
</div> |
|
64 |
<?php elseif($metabox["input_type"] == "textarea") : ?> |
|
65 |
<textarea style="width: 70%;" rows="3" name="<?php echo "obox_".$metabox["name"]; ?>" id="'.$metabox.'"><?php echo $obox_metabox_value; ?></textarea> |
|
66 |
<?php else : ?> |
|
67 |
<input type="text" name="<?php echo "obox_".$metabox["name"]; ?>" id="<?php echo $metabox ?>" value="<?php echo $obox_metabox_value; ?>" size="<?php echo $metabox["input_size"] ?>" /> |
|
68 |
<?php endif; ?> |
|
69 |
<p style="font-size: 11px; clear: both;"><?php echo $metabox["desc"] ?></p> |
|
70 |
</td> |
|
71 |
</tr> |
|
72 |
|
|
73 |
<?php endforeach; ?> |
|
74 |
</table> |
|
75 |
<br /> |
|
76 |
<?php |
|
77 |
} |
|
78 |
|
|
79 |
function insert_obox_metabox($pID) { |
|
80 |
global $obox_meta, $use_file_field, $set_width, $set_height, $image_name; |
|
81 |
foreach ($obox_meta as $metabox) { |
|
82 |
$var = "obox_".$metabox["name"]; |
|
83 |
if (isset($_POST[$var])) : |
|
84 |
if($metabox["input_type"] == "image") : |
|
85 |
$use_file_field = $var."_file"; |
|
86 |
/* Read File Information and desired proportions */ |
|
87 |
$set_width = $metabox["img_width"]; |
|
88 |
$set_height = $metabox["img_height"]; |
|
89 |
$image_name = strtolower(date("dmy")."_".$_FILES[$use_file_field]["name"]); |
|
90 |
|
|
91 |
/* Check if we've actually selected a file */ |
|
92 |
if(!empty($_FILES) && $_FILES[$use_file_field]["name"] !== "") : |
|
93 |
upload_metaimage($use_file_field, $set_width, $set_height, $image_name); |
|
94 |
/* Set post Meta */ |
|
95 |
$meta_name = str_replace("_file", "", $use_file_field); |
|
96 |
$meta_info = get_bloginfo("wpurl")."/wp-content/uploads/".$image_name; |
|
97 |
/* Update Post Meta */ |
|
98 |
add_post_meta($pID, $metabox["name"], $meta_info,true) or update_post_meta($pID, $metabox["name"], $meta_info); |
|
99 |
else : |
|
100 |
/* Update Post Meta */ |
|
101 |
add_post_meta($pID,$metabox["name"],$_POST[$var],true) or update_post_meta($pID,$metabox["name"],$_POST[$var]); |
|
102 |
endif; |
|
103 |
else : |
|
104 |
add_post_meta($pID,$metabox["name"],$_POST[$var],true) or update_post_meta($pID,$metabox["name"],$_POST[$var]); |
|
105 |
endif; |
|
106 |
endif; |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
function add_obox_meta_box() { |
|
111 |
if (function_exists('add_meta_box') ) { |
|
112 |
add_meta_box('obox-meta-box',$GLOBALS['themename'].' Options','create_meta_box_ui','post','normal'); |
|
113 |
} |
|
114 |
} |
|
115 |
function upload_metaimage($use_file_field, $set_width, $set_height, $image_name) { |
|
116 |
global $use_file_field, $set_width, $set_height, $upload_image, $new_image, $mime_type, $resize_type, $resize_height, $resize_width, $resize_percent, $image_name, $image_width, $image_height; |
|
117 |
$file_count = count($_FILES[$use_file_field]); |
|
118 |
/* Upload original first */ |
|
119 |
$final_upload = ABSPATH."wp-content/uploads/".$image_name; |
|
120 |
|
|
121 |
$test = move_uploaded_file($_FILES[$use_file_field]["tmp_name"], $final_upload); |
|
122 |
if($test === true) : |
|
123 |
// Create New Images and Mime Types |
|
124 |
if(strpos($image_name, "gif")) : |
|
125 |
$new_image = imagecreatefromgif($final_upload); |
|
126 |
$mime_type = "gif"; |
|
127 |
elseif(strpos($image_name, "jpg") || strpos($image_name, "jpeg")) : |
|
128 |
$new_image = imagecreatefromjpeg($final_upload); |
|
129 |
$mime_type = "jpg"; |
|
130 |
elseif(strpos($image_name, "png")) : |
|
131 |
$new_image = imagecreatefrompng($final_upload); |
|
132 |
$mime_type = "png"; |
|
133 |
endif; |
|
134 |
|
|
135 |
// Fetch the Original Image Sizes |
|
136 |
list($width, $height) = getimagesize($final_upload); |
|
137 |
$image_width = $width; |
|
138 |
$image_height = $height; |
|
139 |
|
|
140 |
//Set the resize dimensions |
|
141 |
$resize_type = "w"; |
|
142 |
$resize_width = $set_width; |
|
143 |
$resize_height = $set_height; |
|
144 |
$resize_percent = ""; |
|
145 |
$upload_image = $final_upload; |
|
146 |
$add_images = ocmx_save_resample($upload_image, $new_image, $mime_type, $resize_type, $resize_height, $resize_width, $resize_percent, $image_width, $image_height); |
|
147 |
endif; |
|
148 |
} |
|
149 |
|
|
150 |
function ocmx_change_metatype(){ |
|
151 |
?> |
|
152 |
<script type="text/javascript"> |
|
153 |
/* <![CDATA[ */ |
|
154 |
jQuery(document).ready(function(){ |
|
155 |
jQuery('form#post').attr('enctype','multipart/form-data'); |
|
156 |
}); |
|
157 |
/* ]]> */ |
|
158 |
</script> |
|
159 |
<?php } |
|
160 |
|
|
161 |
add_action('admin_menu', 'add_obox_meta_box'); |
|
162 |
add_action('admin_head', 'ocmx_change_metatype'); |
|
163 |
add_action('wp_insert_post', 'insert_obox_metabox'); ?> |