Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. function quiz_mc_post_type_rewrite_rules() {
  2. add_rewrite_tag('%course%','([^&]+)');
  3. add_rewrite_tag('%quiz%', '([^&]+)');
  4. add_rewrite_tag('%quiz_mc_post_type%', '([^/]+)');
  5. add_permastruct('quiz_mc_post_type', '/course/%course%/quiz/%quiz%/question/%quiz_mc_post_type%', true);
  6. add_rewrite_rule('course/([^/]*)/quiz/([^/]*)/question/([^/]*)/?','index.php?post_type=quiz_wc_post_type&course=$matches[1]&quiz=$matches[2]&mc_quiz_question=$matches[3]','top');
  7. }
  8. add_action( 'init', 'quiz_mc_post_type_rewrite_rules' );
  9.  
  10.  
  11. function quiz_mc_post_type() {
  12. $labels = array(
  13. 'name' => _x( 'Multiple Choice Questions', 'Post Type General Name', 'text_domain' ),
  14. 'singular_name' => _x( 'Multiple Choice Question', 'Post Type Singular Name', 'text_domain' ),
  15. 'menu_name' => __( 'Multiple Choice Questions', 'text_domain' ),
  16. 'name_admin_bar' => __( 'Multiple Choice Question', 'text_domain' ),
  17. 'archives' => __( 'Multiple Choice Question Archives', 'text_domain' ),
  18. 'attributes' => __( 'Multiple Choice Question Attributes', 'text_domain' ),
  19. 'parent_item_colon' => __( 'Parent Multiple Choice Question:', 'text_domain' ),
  20. 'all_items' => __( 'All Multiple Choice Questions', 'text_domain' ),
  21. 'add_new_item' => __( 'Add New Multiple Choice Question', 'text_domain' ),
  22. 'add_new' => __( 'Add New', 'text_domain' ),
  23. 'new_item' => __( 'New Multiple Choice Question', 'text_domain' ),
  24. 'edit_item' => __( 'Edit Multiple Choice Question', 'text_domain' ),
  25. 'update_item' => __( 'Update Multiple Choice Question', 'text_domain' ),
  26. 'view_item' => __( 'View Multiple Choice Question', 'text_domain' ),
  27. 'view_items' => __( 'View Multiple Choice Questions', 'text_domain' ),
  28. 'search_items' => __( 'Search Multiple Choice Question', 'text_domain' ),
  29. 'not_found' => __( 'Not found', 'text_domain' ),
  30. 'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
  31. 'featured_image' => __( 'Featured Image', 'text_domain' ),
  32. 'set_featured_image' => __( 'Set featured image', 'text_domain' ),
  33. 'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
  34. 'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
  35. 'insert_into_item' => __( 'Insert into Multiple Choice Question', 'text_domain' ),
  36. 'uploaded_to_this_item' => __( 'Uploaded to this Multiple Choice Question', 'text_domain' ),
  37. 'items_list' => __( 'Multiple Choice Questions list', 'text_domain' ),
  38. 'items_list_navigation' => __( 'Multiple Choice Questions list navigation', 'text_domain' ),
  39. 'filter_items_list' => __( 'Filter Multiple Choice Questions list', 'text_domain' ),
  40. );
  41. $rewrite = array(
  42. 'slug' => 'course/%course%/quiz/%quiz%/question',
  43. 'with_front' => false,
  44. 'pages' => true,
  45. 'feeds' => false,
  46. );
  47. $args = array(
  48. 'label' => __( 'Multiple Choice Question', 'text_domain' ),
  49. 'description' => __( 'Post type used to house quizs', 'text_domain' ),
  50. 'labels' => $labels,
  51. 'supports' => array( 'title', 'editor', 'author', 'revisions', 'post-formats' ),
  52. 'hierarchical' => false,
  53. 'public' => true,
  54. 'show_ui' => true,
  55. 'show_in_menu' => true,
  56. 'menu_position' => 5,
  57. 'show_in_admin_bar' => true,
  58. 'show_in_nav_menus' => false,
  59. 'can_export' => true,
  60. 'has_archive' => false,
  61. 'exclude_from_search' => true,
  62. 'publicly_queryable' => true,
  63. 'query_var' => 'mc_quiz_question',
  64. 'rewrite' => $rewrite,
  65. 'capability_type' => 'post',
  66. );
  67. register_post_type( 'quiz_mc_post_type', $args );
  68. }
  69. add_action( 'init', 'quiz_mc_post_type', 0 );
  70.  
  71. function quiz_mc_post_type_permalinks($permalink, $post, $leavename) {
  72. $post_id = $post->ID;
  73. if($post->post_type != 'quiz_mc_post_type' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
  74. return $permalink;
  75. }
  76. // GETS THE QUESTIONS PARENT WHICH IS A QUIZ
  77. $quiz = $post->post_parent;
  78. $actual_quiz = get_post( $quiz );
  79. $actual_quiz_name = $actual_quiz->post_name;
  80.  
  81. // GETS THE QUIZ PARENT WHICH IS A COURSE
  82. $course = $actual_quiz->post_parent;
  83. $actual_course = get_post( $course );
  84. $actual_course_name = $actual_course->post_name;
  85.  
  86. $needles = array("%course%", "%quiz%");
  87. $replacements = array($actual_course_name, $actual_quiz_name);
  88.  
  89. $permalink = str_replace($needles, $replacements, $permalink);
  90. return $permalink;
  91. }
  92. add_filter('post_type_link', 'quiz_mc_post_type_permalinks', 10, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement