Advertisement
Dfetr

Custom posts

Mar 23rd, 2011
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.92 KB | None | 0 0
  1.  
  2.  
  3. /* ****************************************** START CUSTOM POST TYPES *************************************************** */
  4.  
  5.  
  6. add_action( 'init', 'add_post_type' );
  7.  
  8.  
  9. function add_post_type() {
  10.  
  11. register_post_type('review',
  12.  
  13. array(
  14. 'labels' => array(
  15. 'name' => __( 'Reviews + Custom Fields' ),
  16. 'singular_name' => __( 'Post Your Review' ),
  17. 'add_new' => __( 'Add A New Review' ),
  18. 'add_new_item' => __( 'Add A New Review' ),
  19. 'edit' => __( 'Edit Reviews' ),
  20. 'edit_item' => __( 'Edit Reviews' ),
  21. 'new_item' => __( 'New Review' ),
  22. 'view' => __( 'View This Review' ),
  23. 'view_item' => __( 'View This Review' ),
  24. 'search_items' => __( 'Search Reviews' ),
  25. 'not_found' => __( 'No Reviews Found' ),
  26. 'not_found_in_trash' => __( 'No Reviews Found In Trash' ),
  27. 'parent' => __( 'Parent Review' ),
  28. ),
  29.  
  30.  
  31. 'show_ui' => true,
  32. 'publicly_queryable' => true,
  33. 'exclude_from_search' => false,
  34. 'description' => __( 'Brilliant Reviews ' ),
  35. 'menu_position' => 5,
  36. 'menu_icon' => get_stylesheet_directory_uri() . '/images/review.png',
  37. 'hierarchical' => false,
  38. 'query_var' => true,
  39.  
  40.  
  41. 'capabilities' => array(
  42.  
  43. // 'delete_others_posts' => 'delete_others_reviews',
  44. 'delete_post' => 'delete_review', //METACAPABILITY - DON'T ASSIGN TO A ROLE
  45. 'delete_posts' => 'delete_reviews',
  46. 'delete_private_posts' => 'delete_private_reviews',
  47. 'delete_published_posts' => 'delete_published_reviews',
  48. // 'edit_others_posts' => 'edit_others_reviews',
  49. 'edit_post' => 'edit_review', //METACAPABILITY - DON'T ASSIGN TO A ROLE
  50. 'edit_posts' => 'edit_reviews',
  51. 'edit_private_posts' => 'edit_private_posts',
  52. 'edit_published_posts' => 'edit_published_reviews',
  53. 'publish_posts' => 'publish_reviews',
  54. 'read_post' => 'read_review', //METACAPABILITY - DON'T ASSIGN TO A ROLE
  55. 'read_private_posts' => 'read_private_reviews',
  56. ),
  57.  
  58. 'supports' => array( 'title', 'editor', 'comments','trackbacks', 'revisions','author', 'excerpt', 'thumbnail','custom-fields', 'page-attributes' ),
  59.  
  60. '_builtin' => false,
  61.  
  62. 'rewrite' => array( 'slug' => 'blog', 'with_front' => false ),
  63.  
  64. 'taxonomies' => array('post_tag', 'category', 'other_books'),
  65.  
  66. 'can_export' => true,
  67.  
  68. 'permalink_epmask' => EP_PERMALINK,
  69. //Permalink endpoint mask, whatever that is.
  70.  
  71. )
  72. );
  73. }
  74.  
  75.  
  76. /* ******************************************* END CUSTOM POST TYPES ************************************************ */
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. /* ************************************** START MAPPING META CAPABILITIES ******************************************* */
  86.  
  87. add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 );
  88.  
  89. function my_map_meta_cap( $caps, $cap, $user_id, $args ) {
  90.  
  91. // If editing, deleting, or reading a review, get the post and post type object.
  92. if ( 'edit_review' == $cap || 'delete_review' == $cap || 'read_review' == $cap ) {
  93. $post = get_post( $args[0] );
  94. $post_type = get_post_type_object( $post->post_type );
  95.  
  96. // Set an empty array for the caps.
  97. $caps = array();
  98. }
  99.  
  100. // If editing a review, assign the required capability.
  101. if ( 'edit_review' == $cap ) {
  102. if ( $user_id == $post->post_author )
  103. $caps[] = $post_type->cap->edit_posts;
  104. else
  105. $caps[] = $post_type->cap->edit_others_posts;
  106. }
  107.  
  108. // If deleting a review, assign the required capability.
  109. elseif ( 'delete_review' == $cap ) {
  110. if ( $user_id == $post->post_author )
  111. $caps[] = $post_type->cap->delete_posts;
  112. else
  113. $caps[] = $post_type->cap->delete_others_posts;
  114. }
  115.  
  116. // If reading a private review, assign the required capability.
  117. elseif ( 'read_review' == $cap ) {
  118.  
  119. if ( 'private' != $post->post_status )
  120. $caps[] = 'read';
  121. elseif ( $user_id == $post->post_author )
  122. $caps[] = 'read';
  123. else
  124. $caps[] = $post_type->cap->read_private_posts;
  125. }
  126.  
  127. // Return the capabilities required by the user.
  128. return $caps;
  129. }
  130.  
  131. // With thanks to: http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types#comment-232645
  132.  
  133. /* ************************************ END MAPPING META CAPABILITIES ****************************************** */
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. /* ************************************ START ADDING ROLES **************************************************** */
  145.  
  146. //remove_role('reviewer');
  147.  
  148. $AdaptRole = get_role('subscriber');
  149. $caps = $AdaptRole->capabilities;
  150. add_role('reviewer', 'Reviewer', $caps);
  151.  
  152. $AdaptRole = get_role('reviewer'); // Get the role Reviewer, and then add some of the capabilities below.
  153.  
  154. // $AdaptRole -> add_cap('delete_others_reviews');
  155. $AdaptRole -> add_cap('delete_reviews');
  156. //$AdaptRole -> add_cap('delete_private_reviews');
  157. //$AdaptRole -> add_cap('delete_published_reviews');
  158. // $AdaptRole -> add_cap('edit_others_reviews');
  159. // $AdaptRole -> add_cap('edit_private_reviews');
  160. //$AdaptRole -> add_cap('edit_published_reviews');
  161. //$AdaptRole -> add_cap('publish_reviews');
  162. //$AdaptRole -> add_cap('read_private_reviews');
  163. $AdaptRole -> add_cap('edit_reviews');
  164. $AdaptRole -> add_cap('edit_posts');
  165. $AdaptRole -> add_cap('upload_files');
  166.  
  167.  
  168.  
  169.  
  170. $AdaptRole2 = get_role('administrator'); // So the administrator has access as well :-)
  171.  
  172. $AdaptRole2 -> add_cap('delete_others_reviews');
  173. $AdaptRole2 -> add_cap('delete_reviews');
  174. $AdaptRole2 -> add_cap('delete_private_reviews');
  175. $AdaptRole2 -> add_cap('delete_published_reviews');
  176. $AdaptRole2 -> add_cap('edit_others_reviews');
  177. $AdaptRole2 -> add_cap('edit_reviews');
  178. $AdaptRole2 -> add_cap('edit_private_reviews');
  179. $AdaptRole2 -> add_cap('edit_published_reviews');
  180. $AdaptRole2 -> add_cap('publish_reviews');
  181. $AdaptRole2 -> add_cap('read_private_reviews');
  182.  
  183. /* ***************************************** END ADDING ROLES *************************************************** */
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192. /* *********************************** START CUSTOM TAXONOMIES ************************************************* */
  193.  
  194. function create_my_taxonomies() {
  195.  
  196. register_taxonomy(
  197. 'other_books',
  198. array( 'post', 'page', 'review' ),
  199. array(
  200. 'public' => true,
  201. 'show_ui' => true,
  202. 'show_tagcloud' => true,
  203. 'show_in_nav_menus' => true,
  204. 'hierarchical' => false,
  205. 'labels' => array(
  206. 'name' => __( 'Other Books By This Author' ),
  207. 'singular_name' => __( 'Another Book By This Author' ),
  208. 'search_items' => __( 'Search All Other Books By This Author' ),
  209. 'popular_items' => __( 'Popular Books By This Author' ),
  210. 'all_items' => __( 'All Other Books' ),
  211. 'parent_item' => __( 'Parent Term' ),
  212. 'parent_item_colon' => __( 'Parent Other Books:' ),
  213. 'edit_item' => __( 'Edit This Term' ),
  214. 'update_item' => __( 'Update Other Books' ),
  215. 'add_new_item' => __( 'Add Another Book By This Author' ),
  216. 'new_item_name' => __( 'New Other Books Name' ),
  217. ),
  218. 'query_var' => 'other_books',
  219. 'rewrite' => array( 'slug' => 'otherbookslug', 'with_front' => false),
  220. )
  221. );
  222. }
  223.  
  224. add_action( 'init', 'create_my_taxonomies', 0 ); // Having created your taxonomy with the function create_my_taxonomies this line calls it.
  225.  
  226.  
  227. remove_filter( 'pre_term_description', 'wp_filter_kses' );
  228. // Allow XHTML in term descriptions e.g. the terms in a taxonomy can have a description, and this allows XHTML (such as an image) in that description.
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235. /* ******* Start sending specific terms to a single post (where comments are allowed) ********************* */
  236.  
  237. add_action( 'template_redirect', 'my_redirect_term_to_post' );
  238.  
  239. function my_redirect_term_to_post() {
  240. global $wp_query;
  241.  
  242. if ( is_tax() ) {
  243. $term = $wp_query->get_queried_object();
  244.  
  245.  
  246.  
  247. if ( 'other_books' == $term->taxonomy ) {
  248. $post_id = my_get_post_id_by_slug( $term->slug, 'post' ); // single-default.php will be used to display the content of the post
  249.  
  250. // Change post to page or your custom post type e.g. review e.g. $post_id = my_get_post_id_by_slug( $term->slug, 'review' );
  251. // single-review.php would be used to display the content of the post
  252.  
  253. if ( !empty( $post_id ) )
  254. wp_redirect( get_permalink( $post_id ), 301 );
  255. }
  256. }
  257. }
  258.  
  259.  
  260. function my_get_post_id_by_slug( $slug, $post_type ) {
  261. global $wpdb;
  262.  
  263. $slug = rawurlencode( urldecode( $slug ) );
  264. $slug = sanitize_title( basename( $slug ) );
  265.  
  266.  
  267. $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = %s", $slug, $post_type ) );
  268.  
  269.  
  270. if ( is_array( $post_id ) )
  271. return $post_id[0];
  272. elseif ( !empty( $post_id ) );
  273. return $post_id;
  274.  
  275. return false;
  276. }
  277.  
  278.  
  279. /* ******* End sending specific terms to a single post (where comments are allowed) ********************* */
  280.  
  281.  
  282.  
  283.  
  284. /* ***************************************** END CUSTOM TAXONOMIES ************************************** */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement