Advertisement
Guest User

WordPress meta box with image uploader

a guest
Aug 4th, 2010
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. -------------------------------------------------------------
  2. functions.php
  3. ------------------------------------------------------------
  4.  
  5. <?php
  6. $prefix = 'abc-';
  7.  
  8. $meta_box = array(
  9.  
  10. 'id' => 'my-meta-box',
  11. 'title' => 'My Meta Box',
  12. 'pages' => array('post'),
  13. 'context' => 'normal',
  14. 'priority' => 'high',
  15. 'fields' => array(
  16.  
  17. array(
  18. 'name' => 'Book cover',
  19. 'desc' => 'Add book cover image',
  20. 'id' => 'book-cover',
  21. 'type' => 'text',
  22. ),
  23.  
  24. array(
  25. 'name' => '',
  26. 'desc' => '',
  27. 'id' => 'book-cover-button',
  28. 'type' => 'button',
  29. 'std' => 'Browse'
  30. )
  31.  
  32. )
  33. );
  34.  
  35. foreach ($meta_boxes as $meta_box) {
  36. $my_box = new My_meta_box($meta_box);
  37. }
  38.  
  39. class My_meta_box {
  40.  
  41. protected $_meta_box;
  42.  
  43. // create meta box based on given data
  44. function __construct($meta_box) {
  45. if (!is_admin()) return;
  46.  
  47. $this->_meta_box = $meta_box;
  48.  
  49. // fix upload bug: http://www.hashbangcode.com/blog/add-enctype-wordpress-post-and-page-forms-471.html
  50. $current_page = substr(strrchr($_SERVER['PHP_SELF'], '/'), 1, -4);
  51. if ($current_page == 'page' || $current_page == 'page-new' || $current_page == 'post' || $current_page == 'post-new') {
  52. add_action('admin_head', array(&$this, 'add_post_enctype'));
  53. }
  54.  
  55. add_action('admin_menu', array(&$this, 'add'));
  56.  
  57. add_action('save_post', array(&$this, 'save'));
  58. }
  59.  
  60. function add_post_enctype() {
  61. echo '
  62. <script type="text/javascript">
  63. jQuery(document).ready(function(){
  64. jQuery("#post").attr("enctype", "multipart/form-data");
  65. jQuery("#post").attr("encoding", "multipart/form-data");
  66. });
  67. </script>';
  68. }
  69.  
  70. /// Add meta box for multiple post types
  71. function add() {
  72. foreach ($this->_meta_box['pages'] as $page) {
  73. add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
  74. }
  75. }
  76.  
  77. // Callback function to show fields in meta box
  78. function show() {
  79. global $post;
  80.  
  81. // Use nonce for verification
  82. echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  83.  
  84. echo '<table class="form-table">';
  85.  
  86. foreach ($this->_meta_box['fields'] as $field) {
  87. // get current post meta data
  88. $meta = get_post_meta($post->ID, $field['id'], true);
  89.  
  90. echo '<tr>',
  91. '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  92. '<td>';
  93. switch ($field['type']) {
  94. case 'text':
  95. echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
  96. '<br />', $field['desc'];
  97. break;
  98. case 'textarea':
  99. echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  100. '<br />', $field['desc'];
  101. break;
  102. case 'select':
  103. echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  104. foreach ($field['options'] as $option) {
  105. echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  106. }
  107. echo '</select>';
  108. break;
  109. case 'radio':
  110. foreach ($field['options'] as $option) {
  111. echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  112. }
  113. break;
  114. case 'checkbox':
  115. echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  116. break;
  117. case 'file':
  118. echo $meta ? "$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  119. '<br />', $field['desc'];
  120. break;
  121.  
  122. case 'button':
  123. echo '<input type="button" name="', $field['id'], '" id="', $field['id'], '"value="', $meta ? $meta : $field['std'], '" />';
  124. break;
  125.  
  126. case 'image':
  127. echo $meta ? "<img src=\"$meta\" width=\"150\" height=\"150\" /><br />$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  128. '<br />', $field['desc'];
  129. break;
  130. }
  131. echo '<td>',
  132. '</tr>';
  133. }
  134.  
  135. echo '</table>';
  136. }
  137.  
  138. // Save data from meta box
  139. function save($post_id) {
  140. // verify nonce
  141. if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
  142. return $post_id;
  143. }
  144.  
  145. // check autosave
  146. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  147. return $post_id;
  148. }
  149.  
  150. // check permissions
  151. if ('page' == $_POST['post_type']) {
  152. if (!current_user_can('edit_page', $post_id)) {
  153. return $post_id;
  154. }
  155. } elseif (!current_user_can('edit_post', $post_id)) {
  156. return $post_id;
  157. }
  158.  
  159. foreach ($this->_meta_box['fields'] as $field) {
  160. $name = $field['id'];
  161.  
  162. $old = get_post_meta($post_id, $name, true);
  163. $new = $_POST[$field['id']];
  164.  
  165. if ($field['type'] == 'file' || $field['type'] == 'image') {
  166. $file = wp_handle_upload($_FILES[$name], array('test_form' => false));
  167. $new = $file['url'];
  168. }
  169.  
  170. if ($new && $new != $old) {
  171. update_post_meta($post_id, $name, $new);
  172. } elseif ('' == $new && $old && $field['type'] != 'file' && $field['type'] != 'image') {
  173. delete_post_meta($post_id, $name, $old);
  174. }
  175. }
  176. }
  177. }
  178.  
  179. function my_admin_scripts() {
  180. wp_enqueue_script('media-upload');
  181. wp_enqueue_script('thickbox');
  182. wp_register_script('my-upload', get_bloginfo('template_url') . '/scripts/uploader.js', array('jquery','media-upload','thickbox'));
  183. wp_enqueue_script('my-upload');
  184. }
  185. function my_admin_styles() {
  186. wp_enqueue_style('thickbox');
  187. }
  188. add_action('admin_print_scripts', 'my_admin_scripts');
  189. add_action('admin_print_styles', 'my_admin_styles');
  190.  
  191. ?>
  192.  
  193.  
  194. ------------------------------------------------------------
  195. js file
  196. -------------------------------------------------------------
  197.  
  198. jQuery(document).ready(function() {
  199. jQuery('#book-cover-button').click(function() {
  200. window.send_to_editor = function(html) {
  201. imgurl = jQuery('img',html).attr('src');
  202. jQuery('#book-cover').val(imgurl);
  203. tb_remove();
  204. }
  205. tb_show('', 'media-upload.php?post_id=1&amp;type=image&amp;TB_iframe=true');
  206. return false;
  207. });
  208. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement