Wordpress Costum posttype


SUBMITTED BY: Guest

DATE: July 12, 2013, 8:11 a.m.

FORMAT: PHP

SIZE: 8.7 kB

HITS: 864

  1. <?php
  2. class Custom_Post_Type {
  3. public $post_type_name;
  4. public $post_type_args;
  5. public $post_type_labels;
  6. /* Class constructor */
  7. public function __construct( $name, $args = array(), $labels = array() )
  8. {
  9. // Set some important variables
  10. $this->post_type_name = strtolower( str_replace( ' ', '_', $name ) );
  11. $this->post_type_args = $args;
  12. $this->post_type_labels = $labels;
  13. // Add action to register the post type, if the post type doesnt exist
  14. if( ! post_type_exists( $this->post_type_name ) )
  15. {
  16. add_action( 'init', array( &$this, 'register_post_type' ) );
  17. }
  18. // Listen for the save post hook
  19. $this->save();
  20. }
  21. /* Method which registers the post type */
  22. public function register_post_type()
  23. {
  24. //Capitilize the words and make it plural
  25. $name = ucwords( str_replace( '_', ' ', $this->post_type_name ) );
  26. $plural = $name . "´s";
  27. // We set the default labels based on the post type name and plural. We overwrite them with the given labels.
  28. $labels = array_merge(
  29. // Default
  30. array(
  31. 'name' => _x( $plural, 'post type general name' ),
  32. 'singular_name' => _x( $name, 'post type singular name' ),
  33. 'add_new' => _x( 'Erstellen', strtolower( $name ) ),
  34. 'add_new_item' => __( 'Erstellen ' . $name ),
  35. 'edit_item' => __( 'Bearbeite ' . $name ),
  36. 'new_item' => __( 'Neues ' . $name ),
  37. 'all_items' => __( '' . $plural ),
  38. 'view_item' => __( 'Anzeigen ' . $name ),
  39. 'search_items' => __( 'Suchen ' . $plural ),
  40. 'not_found' => __( 'Keine ' . $plural . ' gefunden'),
  41. 'not_found_in_trash' => __( 'Keine ' . $plural . ' im Papierkorb gefunden'),
  42. 'parent_item_colon' => '',
  43. 'menu_name' => $name
  44. ),
  45. // Given labels
  46. $this->post_type_labels
  47. );
  48. // Same principle as the labels. We set some default and overwite them with the given arguments.
  49. $args = array_merge(
  50. // Default
  51. array(
  52. 'label' => $name,
  53. 'labels' => $labels,
  54. 'public' => true,
  55. 'show_ui' => true,
  56. 'supports' => array('title','editor', 'thumbnail', 'revisions', 'editor' ),
  57. 'show_in_nav_menus' => true,
  58. '_builtin' => false,
  59. ),
  60. // Given args
  61. $this->post_type_args
  62. );
  63. // Register the post type
  64. register_post_type( $this->post_type_name, $args );
  65. }
  66. /* Method to attach the taxonomy to the post type */
  67. public function add_taxonomy( $name, $args = array(), $labels = array() )
  68. {
  69. if( ! empty( $name ) )
  70. {
  71. // We need to know the post type name, so the new taxonomy can be attached to it.
  72. $post_type_name = $this->post_type_name;
  73. // Taxonomy properties
  74. $taxonomy_name = strtolower( str_replace( ' ', '_', $name ) );
  75. $taxonomy_labels = $labels;
  76. $taxonomy_args = $args;
  77. if( ! taxonomy_exists( $taxonomy_name ) )
  78. {
  79. //Capitilize the words and make it plural
  80. $name = ucwords( str_replace( '_', ' ', $name ) );
  81. $plural = $name;
  82. // Default labels, overwrite them with the given labels.
  83. $labels = array_merge(
  84. // Default
  85. array(
  86. 'name' => _x( $plural, 'taxonomy general name' ),
  87. 'singular_name' => _x( $name, 'taxonomy singular name' ),
  88. 'search_items' => __( 'Suchen ' . $plural ),
  89. 'all_items' => __( 'All ' . $plural ),
  90. 'parent_item' => __( 'Eltern ' . $name ),
  91. 'parent_item_colon' => __( 'Eltern ' . $name . ':' ),
  92. 'edit_item' => __( 'Editieren ' . $name ),
  93. 'update_item' => __( 'Speichere ' . $name ),
  94. 'add_new_item' => __( 'Add New ' . $name ),
  95. 'new_item_name' => __( 'New ' . $name . ' Name' ),
  96. 'menu_name' => __( $name ),
  97. ),
  98. // Given labels
  99. $taxonomy_labels
  100. );
  101. // Default arguments, overwitten with the given arguments
  102. $args = array_merge(
  103. // Default
  104. array(
  105. 'label' => $plural,
  106. 'labels' => $labels,
  107. 'public' => true,
  108. 'show_ui' => true,
  109. 'show_in_nav_menus' => true,
  110. '_builtin' => false,
  111. ),
  112. // Given
  113. $taxonomy_args
  114. );
  115. // Add the taxonomy to the post type
  116. add_action( 'init',
  117. function() use( $taxonomy_name, $post_type_name, $args )
  118. {
  119. register_taxonomy( $taxonomy_name, $post_type_name, $args );
  120. }
  121. );
  122. }
  123. else
  124. {
  125. add_action( 'init',
  126. function() use( $taxonomy_name, $post_type_name )
  127. {
  128. register_taxonomy_for_object_type( $taxonomy_name, $post_type_name );
  129. }
  130. );
  131. }
  132. }
  133. }
  134. /* Attaches meta boxes to the post type */
  135. public function add_meta_box( $title, $fields = array(), $context = 'normal', $priority = 'default' )
  136. {
  137. if( ! empty( $title ) )
  138. {
  139. // We need to know the Post Type name again
  140. $post_type_name = $this->post_type_name;
  141. // Meta variables
  142. $box_id = strtolower( str_replace( ' ', '_', $title ) );
  143. $box_title = ucwords( str_replace( '_', ' ', $title ) );
  144. $box_context = $context;
  145. $box_priority = $priority;
  146. // Make the fields global
  147. global $custom_fields;
  148. $custom_fields[$title] = $fields;
  149. add_action( 'admin_init',
  150. function() use( $box_id, $box_title, $post_type_name, $box_context, $box_priority, $fields )
  151. {
  152. add_meta_box(
  153. $box_id,
  154. $box_title,
  155. function( $post, $data )
  156. {
  157. global $post;
  158. // Nonce field for some validation
  159. wp_nonce_field( plugin_basename( __FILE__ ), 'custom_post_type' );
  160. // Get all inputs from $data
  161. $custom_fields = $data['args'][0];
  162. // Get the saved values
  163. $meta = get_post_custom( $post->ID );
  164. // Check the array and loop through it
  165. if( ! empty( $custom_fields ) )
  166. {
  167. /* Loop through $custom_fields */
  168. foreach( $custom_fields as $label => $type )
  169. {
  170. $field_id_name = strtolower( str_replace( ' ', '_', $data['id'] ) ) . '_' . strtolower( str_replace( ' ', '_', $label ) );
  171. echo '<label for="' . $field_id_name . '">' . $label . '</label><input type="text" name="custom_meta[' . $field_id_name . ']" id="' . $field_id_name . '" value="' . $meta[$field_id_name][0] . '" />';
  172. }
  173. }
  174. },
  175. $post_type_name,
  176. $box_context,
  177. $box_priority,
  178. array( $fields )
  179. );
  180. }
  181. );
  182. }
  183. }
  184. /* Listens for when the post type being saved */
  185. public function save()
  186. {
  187. // Need the post type name again
  188. $post_type_name = $this->post_type_name;
  189. add_action( 'save_post',
  190. function() use( $post_type_name )
  191. {
  192. // Deny the wordpress autosave function
  193. if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
  194. if ( ! wp_verify_nonce( $_POST['custom_post_type'], plugin_basename(__FILE__) ) ) return;
  195. global $post;
  196. if( isset( $_POST ) && isset( $post->ID ) && get_post_type( $post->ID ) == $post_type_name )
  197. {
  198. global $custom_fields;
  199. // Loop through each meta box
  200. foreach( $custom_fields as $title => $fields )
  201. {
  202. // Loop through all fields
  203. foreach( $fields as $label => $type )
  204. {
  205. $field_id_name = strtolower( str_replace( ' ', '_', $title ) ) . '_' . strtolower( str_replace( ' ', '_', $label ) );
  206. update_post_meta( $post->ID, $field_id_name, $_POST['custom_meta'][$field_id_name] );
  207. }
  208. }
  209. }
  210. }
  211. );
  212. }
  213. }
  214. ?>

comments powered by Disqus