Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

custommetaimage

By: davidlaw on Mar 13th, 2012  |  syntax: None  |  size: 10.33 KB  |  views: 42  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. $post_meta_boxes = array(
  3. 'thumb' => array(
  4. 'name' => 'thumb',
  5. 'title' => __('Thumbnail:', 'stallion'),
  6. 'type' => 'upload',
  7. 'desc' => 'Upload an image you wish to use as a Thumbnail on archive pages and/or Featured Slideshow Image. See the Stallion Layout options page for more settings. Stallion uses Timthumb 2 to crop and resize images for thumbnails. This means you can use very large images that Stallion will generate small images from saving bandwidth.' ),
  8. 'featured' => array(
  9. 'name' => 'featured',
  10. 'title' => __('Use in Featured Slideshow:', 'stallion'),
  11. 'type' => 'checkbox',
  12. 'desc' => 'Checking this box will add this post to the Featured Slideshow using the above image. It is recommended to use an image size appropriate for the Stallion Sidebar Layout selected under the Stallion Layout Options page<br /><br />310px sidebar layouts - 660px by 300px<br />255px and 165px layouts - 550px by 300px<br />200px wide sidebars - 560px by 300px<br />No Sidebars layout - 970px by 300px.<br /><br />Since Stallion uses Timthumb to zoom/crop images a 660px wide by 300px height will cover all settings except no sidebars.' ),
  13. );
  14. $page_meta_boxes = array(
  15. 'thumb' => array(
  16. 'name' => 'thumb',
  17. 'title' => __('Thumbnail:', 'stallion'),
  18. 'type' => 'upload',
  19. 'desc' => 'Upload an image you wish to use as a Thumbnail on archive pages. See the Stallion Layout options page for more settings. Stallion uses Timthumb 2 to crop and resize images for thumbnails. This means you can use very large images that Stallion will generate small images from saving bandwidth.' ),
  20. );
  21. add_action( 'admin_menu', 'st_create_meta_box' );
  22. add_action( 'save_post', 'st_save_meta_data' );
  23.  
  24. /**
  25.  * Function for adding meta boxes to the admin.
  26.  * Separate the post and page meta boxes.
  27.  
  28. */
  29. function st_create_meta_box() {
  30. global $themename;
  31.  
  32. add_meta_box( 'post-meta-boxes', __($themename.' Stallion Featured Thumbnail Options','stallion'), 'post_meta_boxes', 'post', 'normal', 'high' );
  33. add_meta_box( 'page-meta-boxes', __($themename.' Stallion Featured Thumbnail Options','stallion'), 'page_meta_boxes', 'page', 'normal', 'high' );
  34. }
  35.  
  36. function st_post_meta_boxes() {
  37. global $post_meta_boxes;
  38. return apply_filters( 'st_post_meta_boxes', $post_meta_boxes );
  39. }
  40.  
  41. function st_page_meta_boxes() {
  42. global $page_meta_boxes;
  43. return apply_filters( 'st_page_meta_boxes', $page_meta_boxes );
  44. }
  45.  
  46. function post_meta_boxes() {
  47. global $post;
  48. $meta_boxes = st_post_meta_boxes(); ?>
  49. <table class="form-table">
  50. <?php foreach ( $meta_boxes as $meta ) :
  51.  
  52. $value = get_post_meta( $post->ID, $meta['name'], true );
  53.  
  54. if ( $meta['type'] == 'text' )
  55. get_meta_text_input( $meta, $value );
  56. elseif ( $meta['type'] == 'upload' )
  57. get_meta_upload_input( $meta, $value );
  58. elseif ( $meta['type'] == 'checkbox' )
  59. get_meta_checkbox_input( $meta, $value );
  60. elseif ( $meta['type'] == 'textarea' )
  61. get_meta_textarea( $meta, $value );
  62. elseif ( $meta['type'] == 'select' )
  63. get_meta_select( $meta, $value );
  64.  
  65. endforeach; ?>
  66. </table>
  67. <?php
  68. }
  69.  
  70. /**
  71.  * Displays meta boxes on the Write Page panel.  Loops
  72.  * through each meta box in the $meta_boxes variable.
  73.  * Gets array from st_page_meta_boxes()
  74.  *
  75.  * @since 6.1
  76.  */
  77. function page_meta_boxes() {
  78. global $post;
  79. $meta_boxes = st_page_meta_boxes(); ?>
  80. <table class="form-table">
  81. <?php foreach ( $meta_boxes as $meta ) :
  82.  
  83. $value = stripslashes( get_post_meta( $post->ID, $meta['name'], true ) );
  84.  
  85. if ( $meta['type'] == 'text' )
  86. get_meta_text_input( $meta, $value );
  87. elseif ( $meta['type'] == 'upload' )
  88. get_meta_upload_input( $meta, $value );
  89. elseif ( $meta['type'] == 'checkbox' )
  90. get_meta_checkbox_input( $meta, $value );
  91. elseif ( $meta['type'] == 'textarea' )
  92. get_meta_textarea( $meta, $value );
  93. elseif ( $meta['type'] == 'select' )
  94. get_meta_select( $meta, $value );
  95.  
  96. endforeach; ?>
  97. </table>
  98. <?php
  99. }
  100.  
  101. /**
  102.  * Outputs a text input box with arguments from the
  103.  * parameters.  Used for both the post/page meta boxes.
  104.  *
  105.  * @since 6.1
  106.  * @param array $args
  107.  * @param array string|bool $value
  108.  */
  109. function get_meta_text_input( $args = array(), $value = false ) {
  110.  
  111. extract( $args ); ?>
  112. <tr>
  113. <th style="width:10%;">
  114. <label for="<?php echo $name; ?>"><?php echo $title; ?></label>
  115. </th>
  116. <td>
  117. <?php /* <input type="text" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="<?php echo wp_specialchars( $value, 1 ); ?>" size="30" tabindex="30" style="width: 97%;" /> */?>
  118. <input type="text" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="<?php echo esc_html( $value, 1 ); ?>" size="30" tabindex="30" style="width: 97%;" />
  119. <input type="hidden" name="<?php echo $name; ?>_noncename" id="<?php echo $name; ?>_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
  120. <p><?php echo $desc ;?></p>
  121. </td>
  122. </tr>
  123. <?php
  124. }
  125. /**
  126.  * Outputs a text input box with arguments from the
  127.  * parameters.  Used for both the post/page meta boxes.
  128.  *
  129.  * @since 6.1
  130.  * @param array $args
  131.  * @param array string|bool $value
  132.  */
  133. function get_meta_upload_input( $args = array(), $value = false ) {
  134.  
  135. extract( $args ); ?>
  136. <script type="text/javascript" language="javascript">
  137.  
  138. jQuery(document).ready(function() {
  139.  
  140. var header_clicked = "false";
  141.  
  142. jQuery('#<?php echo $name; ?>_button').click(function() {
  143. jQuery('html').addClass('<?php echo $name; ?>');
  144. formfield = jQuery('#<?php echo $name; ?>').attr('name');
  145. tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
  146. header_clicked = "true";
  147. return false;
  148. });
  149.  
  150. // user inserts file into post. only run custom if user started process using the above process
  151. // window.send_to_editor(html) is how wp would normally handle the received data
  152.  
  153. window.original_send_to_editor = window.send_to_editor;
  154. window.send_to_editor = function(html){
  155.  
  156. if (header_clicked == "true") {
  157. fileurl = jQuery('img',html).attr('src');
  158. jQuery('#<?php echo $name; ?>').val(fileurl);
  159. tb_remove();
  160. header_clicked = "false";
  161. jQuery('html').removeClass('<?php echo $name; ?>');
  162. } else {
  163. window.original_send_to_editor(html);
  164. }
  165. };
  166.  
  167. });
  168.  
  169. </script>
  170. <tr>
  171. <th style="width:10%;">
  172. <label for="upload_<?php echo $name; ?>"><?php echo $title; ?></label>
  173. </th>
  174. <td>
  175. <?php /* <input size="40" type="text" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="<?php echo wp_specialchars( $value, 1 ); ?>" /> */?>
  176. <input size="40" type="text" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="<?php echo esc_html( $value, 1 ); ?>" />
  177.  
  178. <input id="<?php echo $name; ?>_button" class="upload_button button" type="button" value="<?php _e('Upload Image','stallion');?>" rel="300" />
  179.  
  180. <input type="hidden" name="<?php echo $name; ?>_noncename" id="<?php echo $name; ?>_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
  181. <p><?php echo $desc ;?></p>
  182. </td>
  183. </tr>
  184. <?php
  185. }
  186. /**
  187.  * Outputs a select box with arguments from the
  188.  * parameters.  Used for both the post/page meta boxes.
  189.  *
  190.  * @since 6.1
  191.  * @param array $args
  192.  * @param array string|bool $value
  193.  */
  194. function get_meta_select( $args = array(), $value = false ) {
  195. extract( $args ); ?>
  196. <tr>
  197. <th style="width:10%;">
  198. <label for="<?php echo $name; ?>"><?php echo $title; ?></label>
  199. </th>
  200. <td>
  201. <select name="<?php echo $name; ?>" id="<?php echo $name; ?>">
  202. <?php foreach ( $options as $option ) : ?>
  203. <option <?php if ( htmlentities( $value, ENT_QUOTES ) == $option ) echo ' selected="selected"'; ?>>
  204. <?php echo $option; ?>
  205. </option>
  206. <?php endforeach; ?>
  207. </select>
  208. <input type="hidden" name="<?php echo $name; ?>_noncename" id="<?php echo $name; ?>_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
  209. </td>
  210. </tr>
  211. <?php
  212. }
  213. /**
  214.  * Outputs a textarea with arguments from the
  215.  * parameters.  Used for both the post/page meta boxes.
  216.  *
  217.  * @since 6.1
  218.  * @param array $args
  219.  * @param array string|bool $value
  220.  */
  221. function get_meta_textarea( $args = array(), $value = false ) {
  222. extract( $args ); ?>
  223. <tr>
  224. <th style="width:10%;">
  225. <label for="<?php echo $name; ?>"><?php echo $title; ?></label>
  226. </th>
  227. <td>
  228. <?php /* <textarea name="<?php echo $name; ?>" id="<?php echo $name; ?>" cols="60" rows="4" tabindex="30" style="width: 97%;"><?php echo wp_specialchars( $value, 1 ); ?></textarea> */?>
  229. <textarea name="<?php echo $name; ?>" id="<?php echo $name; ?>" cols="60" rows="4" tabindex="30" style="width: 97%;"><?php echo esc_html( $value, 1 ); ?></textarea>
  230. <input type="hidden" name="<?php echo $name; ?>_noncename" id="<?php echo $name; ?>_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
  231. <p><?php echo $desc ;?></p>
  232. </td>
  233. </tr>
  234. <?php
  235. }
  236. function get_meta_checkbox_input( $args = array(), $value = false ) {
  237. global $post_id;
  238. extract( $args ); ?>
  239. <tr>
  240. <th style="width:10%;">
  241. <label for="<?php echo $name; ?>"><?php echo $title; ?></label>
  242. </th>
  243. <td>
  244. <?php
  245. if(get_post_meta( $post_id, $name, true ) == 'true')
  246. $checked = ' checked="checked"';
  247. else
  248. $checked = '';
  249. ?>
  250. <input type="checkbox" name="<?php echo $name; ?>" value="true" <?php echo $checked; ?> /> <span><?php echo $desc ;?></span>
  251.  
  252. <input type="hidden" name="<?php echo $name; ?>_noncename" id="<?php echo $name; ?>_noncename" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
  253. </td>
  254. </tr>
  255. <?php
  256. }
  257. /**
  258.  * Loops through each meta box set of variables.
  259.  * Saves them to the database as custom fields.
  260.  *
  261.  * @since 6.1
  262.  * @param int $post_id
  263.  */
  264. function st_save_meta_data( $post_id ) {
  265. global $post;
  266.  
  267. if ( 'page' == $_POST['post_type'] )
  268. $meta_boxes = array_merge( st_page_meta_boxes() );
  269. else
  270. $meta_boxes = array_merge( st_post_meta_boxes() );
  271.  
  272. foreach ( $meta_boxes as $meta_box ) :
  273.  
  274. if ( !wp_verify_nonce( $_POST[$meta_box['name'] . '_noncename'], plugin_basename( __FILE__ ) ) )
  275. return $post_id;
  276.  
  277. if ( 'page' == $_POST['post_type'] && !current_user_can( 'edit_page', $post_id ) )
  278. return $post_id;
  279.  
  280. elseif ( 'post' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) )
  281. return $post_id;
  282.  
  283. $data = stripslashes( $_POST[$meta_box['name']] );
  284.  
  285. if ( get_post_meta( $post_id, $meta_box['name'] ) == '' )
  286. add_post_meta( $post_id, $meta_box['name'], $data, true );
  287.  
  288. elseif ( $data != get_post_meta( $post_id, $meta_box['name'], true ) )
  289. update_post_meta( $post_id, $meta_box['name'], $data );
  290.  
  291. elseif ( $data == '' )
  292. delete_post_meta( $post_id, $meta_box['name'], get_post_meta( $post_id, $meta_box['name'], true ) );
  293. endforeach;
  294. }
  295. ?>