Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. // Register Custom Post Types
  2.  
  3. function my_register_cpt() {
  4.  
  5. $labels = array(
  6. 'name' => _x( 'symposium', 'Post Type General Name', 'gbc' ),
  7. 'singular_name'=> _x( 'Symposium', 'Post Type Singular Name', 'gbc' ),
  8. 'menu_name' => __( 'Symposiums', 'gbc' ),
  9. 'name_admin_bar => __( 'Symposiums', 'gbc' ),
  10. 'all_items' => __( 'All Symposiums', 'gbc' ),
  11. 'view_item' => __( 'View Symposium', 'gbc' ),
  12. 'add_new_item' => __( 'Add New Symposium', 'gbc' ),
  13. 'add_new' => __( 'Add New', 'gbc' ),
  14. 'edit_item' => __( 'Edit Symposium', 'gbc' ),
  15. 'update_item' => __( 'Update Symposium', 'gbc' ),
  16. 'search_items' => __( 'Search Symposium', 'gbc' ),
  17. 'not_found' => __( 'Not Found', 'gbc' ),
  18. 'not_found_in_trash' => __( 'Not found in Trash', 'gbc' ),
  19. );
  20.  
  21. $args = array(
  22. 'label' => __( 'Symposium', '1fix' ),
  23. 'labels' => $labels,
  24. 'hierarchical' => true,
  25. 'public' => true,
  26. 'menu_icon' => 'dashicons-carrot',
  27. 'menu_position' => 5,
  28. 'supports' => array( 'title', 'editor','thumbnail', 'page-attributes' ),
  29. );
  30.  
  31. register_post_type( 'symposium', $args );
  32.  
  33.  
  34.  
  35. $labels = array(
  36. 'name' => _x( 'presentation', 'Post Type General Name', 'gbc' ), 'singular_name' => _x( 'Presentations', 'Post Type Singular Name', 'gbc' ),
  37. 'menu_name' => __( 'Presentations', 'gbc' ),
  38. 'name_admin_bar' => __( 'Presentations', 'gbc' ),
  39. 'all_items' => __( 'All Presentations', 'gbc' ),
  40. 'view_item' => __( 'View Presentation', 'gbc' ),
  41. 'add_new_item' => __( 'Add New Presentation', 'gbc' ),
  42. 'add_new' => __( 'Add New', 'gbc' ),
  43. 'edit_item' => __( 'Edit Presentation', 'gbc' ),
  44. 'update_item' => __( 'Update Presentation', 'gbc' ),
  45. 'search_items' => __( 'Search Presentation', 'gbc' ),
  46. 'not_found' => __( 'Not Found', 'gbc' ),
  47. 'not_found_in_trash' => __( 'Not found in Trash', 'gbc' ),);
  48.  
  49. $args = array(
  50. 'label' => __( 'Presentations', 'gbc' ),
  51. 'labels' => $labels,
  52. 'hierarchical' => false,
  53. 'public' => true,
  54. 'rewrite' => array(
  55. 'slug' => 'presentation',
  56. 'with_front' => false,
  57. ),
  58. 'menu_icon' => 'dashicons-money',
  59. 'show_in_menu' => 'edit.php?post_type=symposium'
  60. );
  61.  
  62. register_post_type( 'presentation', $args );
  63.  
  64.  
  65.  
  66. $labels = array(
  67. 'name' => _x( 'speaker', 'Post Type General Name', 'gbc' ),
  68. 'singular_name' => _x( 'Speaker', 'Post Type Singular Name', 'gbc' ),
  69. 'menu_name' => __( 'Speakers', 'gbc' ),
  70. 'name_admin_bar' => __( 'Speakers', 'gbc' ),
  71. 'all_items' => __( 'All Speakers', 'gbc' ),
  72. 'view_item' => __( 'View Speaker', 'gbc' ),
  73. 'add_new_item' => __( 'Add New Speaker', 'gbc'),
  74. 'add_new' => __( 'Add New', 'gbc' ),
  75. 'edit_item' => __( 'Edit Speaker', 'gbc' ),
  76. 'update_item' => __( 'Update Speaker', 'gbc' ),
  77. 'search_items' => __( 'Search Speaker', 'gbc' ),
  78. 'not_found' => __( 'Not Found', 'gbc' ),
  79. 'not_found_in_trash' => __( 'Not found in Trash', 'gbc' ),
  80. );
  81.  
  82. $args = array(
  83. 'label' => __( 'Speakers', 'gbc' ),
  84. 'labels' => $labels,
  85. 'hierarchical' => false,
  86. 'public' => true,
  87. 'rewrite' => array(
  88. 'slug' => 'speaker',
  89. 'with_front' => false,),
  90. 'menu_icon' => 'dashicons-groups',
  91. 'show_in_menu' => 'edit.php?post_type=symposium',
  92. 'show_in_rest' => true,
  93. 'supports' => array( 'title', 'editor','thumbnail','page-attributes' ));
  94.  
  95. register_post_type( 'speaker', $args );
  96.  
  97.  
  98.  
  99. $labels = array(
  100. 'name' => _x( 'session', 'Post Type General Name', 'gbc' ),
  101. 'singular_name' => _x( 'Session', 'Post Type Singular Name', 'gbc' ),
  102. 'menu_name' => __( 'Sessions', 'gbc' ),
  103. 'name_admin_bar' => __( 'Sessions', 'gbc' ));
  104.  
  105. $args = array(
  106.  
  107. 'label' => __( 'Sessions', 'gbc' ),
  108. 'labels' => $labels,
  109. 'hierarchical' => false,
  110. 'public' => true,
  111. 'menu_icon' => 'dashicons-media-video',
  112. 'show_in_menu' => 'edit.php?post_type=symposium'
  113. );
  114.  
  115. register_post_type( 'session', $args );
  116.  
  117. }
  118.  
  119. add_action( 'init', 'my_register_cpt' );
  120.  
  121. function my_add_rewrite_rules() {
  122. global $wp_rewrite;
  123.  
  124. // Presentation rewrite
  125. add_rewrite_tag('%presentation%', '([^/]+)', 'presentation=');
  126. add_permastruct('presentation','/%symposium%/presentation/%presentation%', false);
  127. add_rewrite_rule('^presentation/([^/]+)/([^/]+)/?','index.php?presentation=$matches[2]','top');
  128.  
  129. // Speaker rewrite
  130. add_rewrite_tag('%speaker%', '([^/]+)', 'speaker=');
  131. add_permastruct('speaker', '/%symposium%/speaker/%speaker%', false);
  132. add_rewrite_rule('^speaker/?([^/]+)/([^/]+)/?','index.php?speaker=$matches[2]','top');
  133.  
  134. $wp_rewrite->flush_rules();
  135.  
  136. }
  137.  
  138. add_action( 'init', 'my_add_rewrite_rules' );
  139.  
  140. function my_permalinks($permalink, $post, $leavename) {
  141. $post_id = $post->ID;
  142.  
  143. if($post->post_type != 'speaker' && $post->post_type != 'presentation' && $post->post_type != 'session' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
  144. return $permalink;
  145.  
  146. } else {
  147.  
  148. $parent = $post->post_parent;
  149. $parent_post = get_post( $parent );
  150. $permalink = str_replace('%symposium%', $parent_post->post_name, $permalink);
  151. return $permalink;
  152.  
  153. }
  154. }
  155.  
  156. add_filter('post_type_link', 'my_permalinks', 10, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement