Custom Post Type


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function custom_event_type_css() {
  2. // This makes sure that the posinioning is also good for right-to-left languages
  3. $x = ( 'rtl' == get_bloginfo( 'text_direction' ) ) ? 'left' : 'right';
  4.  
  5. echo "
  6. <style type='text/css'>
  7. #future .form-table td input{margin:0 5px 0 10px;}
  8. #time .form-table .timestuff {float:left; width:65%;}
  9. #time .form-table tr span.timefield {float:left;}
  10. #time .form-table tr span.timeselect {float:right;}
  11. #date .form-table .timestuff {float:left; width:85%;}
  12. #date .form-table tr span.timefield {float:left;}
  13. #date .form-table tr span.timeselect {float:right;}
  14. #date .form-table tr span.timeselect select {width:130px;}
  15. }
  16. </style>
  17. ";
  18. }
  19.  
  20. add_action('admin_head', 'custom_event_type_css');
  21.  
  22.  
  23.  
  24. add_action( 'init', 'headline_post_types' );
  25.  
  26. function headline_post_types() {
  27. register_post_type( 'headline_list',
  28. 'labels' => array(
  29. 'name' => __( 'Headlines' ),
  30. 'singular_name' => __( 'Headline' ),
  31. 'add_new' => __( 'Add New' ),
  32. 'add_new_item' => __( 'Add New Headline' ),
  33. 'edit' => __( 'Edit' ),
  34. 'edit_item' => __( 'Edit Headline' ),
  35. 'new_item' => __( 'New Headline' ),
  36. 'view' => __( 'View Headline' ),
  37. 'view_item' => __( 'View Headline' ),
  38. 'search_items' => __( 'Search Headline' ),
  39. 'not_found' => __( 'No headline found' ),
  40. 'not_found_in_trash' => __( 'No headline found in Trash' ),
  41. ),
  42. 'public' => true,
  43. 'show_ui' => true,
  44. 'publicly_queryable' => true,
  45. 'exclude_from_search' => false,
  46. 'menu_position' => 5,
  47. 'capability_type' => 'post',
  48. 'menu_icon' => get_stylesheet_directory_uri() . '/images/headlines_menu.png',
  49. 'query_var' => true,
  50. 'rewrite' => array( 'slug' => 'headline', 'with_front' => false ),
  51. 'supports' => array('title', 'author', 'revisions', 'custom-fields' ),
  52. )
  53. );
  54. }
  55.  
  56.  
  57.  
  58. $meta_boxes = array();
  59.  
  60. //Headline Info Box
  61.  
  62.  
  63. $meta_boxes[] = array(
  64. 'id' => 'summary',
  65. 'title' => 'Headline Summary',
  66. 'pages' => array('headline_list'), // custom post type
  67. 'context' => 'normal',
  68. 'priority' => 'high',
  69. 'fields' => array(
  70. 'name' => 'Headline Summary',
  71. 'desc' => 'Enter a brief summary of the headline',
  72. 'id' => $prefix . 'headline_summ',
  73. 'type' => 'textarea_sm',
  74. 'std' => ''
  75. )
  76. )
  77. );
  78.  
  79. // Headline Link Box
  80. $meta_boxes[] = array(
  81. 'id' => 'link',
  82. 'title' => 'Headline Link',
  83. 'pages' => array('headline_list'), // custom post type
  84. 'context' => 'normal',
  85. 'priority' => 'high',
  86. 'fields' => array(
  87. 'name' => 'Headline Link',
  88. 'desc' => 'Enter the link to the headline',
  89. 'id' => $prefix . 'event_link',
  90. 'type' => 'text',
  91. 'std' => ''
  92. )
  93. )
  94. );
  95.  
  96. // Headline Source Box
  97. $meta_boxes[] = array(
  98. 'id' => 'source',
  99. 'title' => 'Headline Source',
  100. 'pages' => array('headline_list'), // custom post type
  101. 'context' => 'normal',
  102. 'priority' => 'high',
  103. 'fields' => array(
  104. 'name' => 'Headline Source',
  105. 'desc' => 'Enter the source of the headline (i.e. newspaper, magazine, etc)',
  106. 'id' => $prefix . 'event_source',
  107. 'type' => 'text',
  108. 'std' => ''
  109. )
  110. )
  111. );
  112.  
  113. /*********************************
  114.  
  115. You should not edit the code below
  116.  
  117. *********************************/
  118.  
  119. foreach ($meta_boxes as $meta_box) {
  120. $my_box = new My_meta_box($meta_box);
  121. }
  122.  
  123. class My_meta_box {
  124.  
  125. protected $_meta_box;
  126.  
  127. // create meta box based on given data
  128. function __construct($meta_box) {
  129. if (!is_admin()) return;
  130.  
  131. $this->_meta_box = $meta_box;
  132.  
  133. // fix upload bug: http://www.hashbangcode.com/blog/add-enctype-wordpress-post-and-page-forms-471.html
  134. $current_page = substr(strrchr($_SERVER['PHP_SELF'], '/'), 1, -4);
  135. if ($current_page == 'page' || $current_page == 'page-new' || $current_page == 'post' || $current_page == 'post-new') {
  136. add_action('admin_head', array(&$this, 'add_post_enctype'));
  137. }
  138.  
  139. add_action('admin_menu', array(&$this, 'add'));
  140.  
  141. add_action('save_post', array(&$this, 'save'));
  142. }
  143.  
  144. function add_post_enctype() {
  145. echo '
  146. <script type="text/javascript">
  147. jQuery(document).ready(function(){
  148. jQuery("#post").attr("enctype", "multipart/form-data");
  149. jQuery("#post").attr("encoding", "multipart/form-data");
  150. });
  151. </script>';
  152. }
  153.  
  154. /// Add meta box for multiple post types
  155. function add() {
  156. foreach ($this->_meta_box['pages'] as $page) {
  157. add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
  158. }
  159. }
  160.  
  161. // Callback function to show fields in meta box
  162. function show() {
  163. global $post;
  164.  
  165. // Use nonce for verification
  166. echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  167.  
  168. echo '<table class="form-table">';
  169.  
  170. foreach ($this->_meta_box['fields'] as $field) {
  171. // get current post meta data
  172. $meta = get_post_meta($post->ID, $field['id'], true);
  173.  
  174. echo '<tr>',
  175. //'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  176. '<td>';
  177. switch ($field['type']) {
  178. case 'text':
  179. echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
  180. '<br />', $field['desc'];
  181. break;
  182. case 'textarea':
  183. echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="10" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  184. '<br />', $field['desc'];
  185. break;
  186. case 'textarea_sm':
  187. echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="2" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  188. '<br />', $field['desc'];
  189. break;
  190. case 'selecttime':
  191. echo '<div class="timestuff">','<span class="timefield">',$field['desc'],'</span>', '<span class="timeselect">', '<select name="', $field['id'], '" id="', $field['id'], '">','</span>','</div>';
  192. foreach ($field['options'] as $option) {
  193. echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  194. }
  195. echo '</select>';
  196. break;
  197. case 'select':
  198. echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  199. foreach ($field['options'] as $option) {
  200. echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  201. }
  202. echo '</select>';
  203. break;
  204. case 'radio':
  205. foreach ($field['options'] as $option) {
  206. echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  207. }
  208. break;
  209. case 'checkbox':
  210. echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />', $field['desc'];
  211. break;
  212. case 'file':
  213. echo $meta ? "$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  214. '<br />', $field['desc'];
  215. break;
  216. case 'image':
  217. echo $meta ? "<img src=\"$meta\" width=\"150\" height=\"150\" /><br />$meta<br />" : '', '<input type="file" name="', $field['id'], '" id="', $field['id'], '" />',
  218. '<br />', $field['desc'];
  219. break;
  220. }
  221. echo '<td>',
  222. '</tr>';
  223. }
  224.  
  225. echo '</table>';
  226. }
  227.  
  228. // Save data from meta box
  229. function save($post_id) {
  230. // verify nonce
  231. if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
  232. return $post_id;
  233. }
  234.  
  235. // check autosave
  236. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  237. return $post_id;
  238. }
  239.  
  240. // check permissions
  241. if ('page' == $_POST['post_type']) {
  242. if (!current_user_can('edit_page', $post_id)) {
  243. return $post_id;
  244. }
  245. } elseif (!current_user_can('edit_post', $post_id)) {
  246. return $post_id;
  247. }
  248.  
  249. foreach ($this->_meta_box['fields'] as $field) {
  250. $name = $field['id'];
  251.  
  252. $old = get_post_meta($post_id, $name, true);
  253. $new = $_POST[$field['id']];
  254.  
  255. if ($field['type'] == 'file' || $field['type'] == 'image') {
  256. $file = wp_handle_upload($_FILES[$name], array('test_form' => false));
  257. $new = $file['url'];
  258. }
  259.  
  260. if ($new && $new != $old) {
  261. update_post_meta($post_id, $name, $new);
  262. } elseif ('' == $new && $old && $field['type'] != 'file' && $field['type'] != 'image') {
  263. delete_post_meta($post_id, $name, $old);
  264. }
  265. }
  266. }
  267. }
  268.  
  269. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.