Posted By

simnor on 01/01/11


Tagged

image wordpress upload fields field custom


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

Sandeepbg
qubestream
estevancarlos
rmmjr


Custom fields with Upload image option - WordPress


 / Published in: PHP
 

Original custom fields code based on Wefunction's code here: http://wefunction.com/2008/10/tutorial-creating-custom-write-panels-in-wordpress/ enhanced to add support for uploading images.

  1. <?php
  2. if ( is_admin() ) {
  3. function add_post_enctype() {
  4. echo "<script type='text/javascript'>
  5. jQuery(document).ready(function(){
  6. jQuery('#post').attr('enctype','multipart/form-data');
  7. });
  8. </script>";
  9. }
  10. add_action('admin_head', 'add_post_enctype');
  11. }
  12.  
  13. $new_meta_boxes =
  14.  
  15. "start" => array(
  16. "name" => "start",
  17. "type" => "start"),
  18.  
  19.  
  20. "image" => array(
  21. "name" => "image",
  22. "std" => "",
  23. "type" => "image",
  24. "title" => "Thumbnail Image"),
  25.  
  26.  
  27. "bannerimage" => array(
  28. "name" => "bannerimage",
  29. "std" => "",
  30. "type" => "image",
  31. "title" => "Banner Image"),
  32.  
  33.  
  34. "tagline" => array(
  35. "name" => "tagline",
  36. "std" => "",
  37. "type" => "text",
  38. "title" => "Tagline",
  39. "description" => "Tagline to your post"),
  40.  
  41.  
  42. "end" => array(
  43. "name" => "end",
  44. "type" => "end"),
  45. );
  46.  
  47.  
  48. function new_meta_boxes() {
  49. global $post, $new_meta_boxes;
  50.  
  51. foreach($new_meta_boxes as $meta_box) {
  52. $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'', true);
  53.  
  54. if($meta_box_value == "")
  55. $meta_box_value = $meta_box['std'];
  56.  
  57. echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
  58. if($meta_box['type'] == "start") {
  59.  
  60. echo "<div class='optionsbox'><style type='text/css'>.optionsbox {
  61. display:block;
  62. width:auto;
  63. float:none;
  64. overflow: hidden;
  65. }
  66.  
  67. .optionsbox input, .optionsbox textarea {
  68. outline:none;
  69. padding:5px;
  70. color:#999;
  71. }
  72.  
  73. .optionsbox input:focus, .optionsbox textarea:focus {
  74. border-color:#999;
  75. color:#666;
  76. }
  77.  
  78. .optionsbox p {
  79. margin-bottom:20px;
  80. }
  81.  
  82. .optionsbox label {
  83. width:140px;
  84. display:block;
  85. float:left;
  86. margin-top:3px;
  87. }
  88.  
  89. .optionsbox small {
  90. padding-left:140px;
  91. padding-top:3px;
  92. color:#999;
  93. }
  94. </style>";
  95.  
  96. } else if($meta_box['type'] == "end") {
  97.  
  98. echo '</div>';
  99.  
  100. } else if($meta_box['type'] == "image") {
  101.  
  102. echo $meta_box['before'];
  103.  
  104. echo '<div style="background:#f4f4f4;padding:10px;height:120px;margin:0 0 20px 0;display:block">';
  105.  
  106. if($meta_box_value) { echo '<img style="float:right" src="'.get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$meta_box_value.'&w=120&h=120" alt="" />'; }
  107.  
  108. echo'<p><label for="'.$meta_box['name'].'_upload">'.$meta_box['title'].'</label>';
  109. echo'<input type="file" name="'.$meta_box['name'].'_upload" size="55" /><br />';
  110. echo'<small>Upload image here</small></p>';
  111.  
  112. echo'<p><label>&nbsp;</label>';
  113. echo'<input type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
  114. echo'<small>or add a URL to the image here</small></p>';
  115.  
  116. echo '</div>';
  117.  
  118. } else if($meta_box['type'] == "text") {
  119.  
  120. echo $meta_box['before'];
  121.  
  122. echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
  123. echo'<input style="color:#666;" type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
  124. echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
  125.  
  126. } else if($meta_box['type'] == "checkbox") {
  127.  
  128. echo $meta_box['before'];
  129.  
  130. echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
  131. if($meta_box_value) { $checked = "checked=\"checked\""; } else { $checked = ""; }
  132. echo '<input style="display:block;float:left;width:20px;margin:5px 0 0 0;" '.$checked.' type="checkbox" name="'.$meta_box['name'].'" /><br/>';
  133. echo'<small style="clear:both;padding-left:140px;padding-top:3px;display:block;">'.$meta_box['description'].'</small></p>';
  134.  
  135. } else if($meta_box['type'] == "textarea") {
  136.  
  137. echo $meta_box['before'];
  138. echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
  139. echo'<textarea style="color:#666;" name="'.$meta_box['name'].'" cols="50" rows="4">'.stripslashes($meta_box_value).'</textarea><br />';
  140. echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
  141.  
  142. }
  143.  
  144. }
  145. }
  146.  
  147.  
  148. function create_meta_box() {
  149. global $theme_name;
  150. if ( function_exists('add_meta_box') ) {
  151. add_meta_box( 'new-meta-boxes', 'Post Options', 'new_meta_boxes', 'post', 'normal', 'high' );
  152. add_meta_box( 'new-meta-boxes', 'Page Options', 'new_meta_boxes', 'page', 'normal', 'high' );
  153. }
  154. }
  155.  
  156.  
  157. function save_postdata( $post_id ) {
  158. global $post, $new_meta_boxes;
  159. $post_id = wp_is_post_revision($post_id);
  160.  
  161. foreach($new_meta_boxes as $meta_box) {
  162.  
  163. if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
  164. return $post_id;
  165. }
  166.  
  167. if ( 'page' == $_POST['post_type'] ) {
  168. if ( !current_user_can( 'edit_page', $post_id ))
  169. return $post_id;
  170. } else {
  171. if ( !current_user_can( 'edit_post', $post_id ))
  172. return $post_id;
  173. }
  174.  
  175. $imageuploadlocation = "";
  176. $metaboxname = "";
  177. $metaboxname_upload = "";
  178.  
  179. if($meta_box['type'] == 'image') {
  180.  
  181. $metaboxname = $meta_box['name'];
  182. $metaboxname_upload = $metaboxname.'_upload';
  183.  
  184. if($_FILES[$metaboxname_upload]['name'] != "") {
  185. $overrides = array( 'test_form' => false);
  186. $imagefile=wp_handle_upload($_FILES[$metaboxname_upload], $overrides);
  187. $imageuploadlocation = $imagefile['url'];
  188. delete_post_meta($post_id, $metaboxname, get_post_meta($post_id, $metaboxname, true));
  189. add_post_meta($post_id, $metaboxname, $imageuploadlocation, true);
  190. } else {
  191. $imageuploadlocation = get_post_meta($post_id, $metaboxname, true);
  192. delete_post_meta($post_id, $metaboxname, get_post_meta($post_id, $metaboxname, true));
  193. add_post_meta($post_id, $metaboxname, $_POST[$metaboxname], true);
  194. }
  195.  
  196.  
  197. } else {
  198.  
  199. $data = $_POST[$meta_box['name'].''];
  200. if(get_post_meta($post_id, $meta_box['name'].'') == "")
  201. add_post_meta($post_id, $meta_box['name'].'', $data, true);
  202. elseif($data != get_post_meta($post_id, $meta_box['name'].'', true))
  203. update_post_meta($post_id, $meta_box['name'].'', $data);
  204. elseif($data == "")
  205. delete_post_meta($post_id, $meta_box['name'].'', get_post_meta($post_id, $meta_box['name'].'', true));
  206.  
  207. }
  208.  
  209.  
  210. }
  211.  
  212. }
  213.  
  214.  
  215.  
  216. add_action('admin_menu', 'create_meta_box');
  217. add_action('save_post', 'save_postdata', 12);?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: seanthom2004 on February 26, 2011

Do you have any screen shots of this just so I can get an idea of what this ends up looking like?

Thanks

You need to login to post a comment.