Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function quiz_mc_post_type_rewrite_rules() {
- add_rewrite_tag('%course%','([^&]+)');
- add_rewrite_tag('%quiz%', '([^&]+)');
- add_rewrite_tag('%quiz_mc_post_type%', '([^/]+)');
- add_permastruct('quiz_mc_post_type', '/course/%course%/quiz/%quiz%/question/%quiz_mc_post_type%', true);
- 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');
- }
- add_action( 'init', 'quiz_mc_post_type_rewrite_rules' );
- function quiz_mc_post_type() {
- $labels = array(
- 'name' => _x( 'Multiple Choice Questions', 'Post Type General Name', 'text_domain' ),
- 'singular_name' => _x( 'Multiple Choice Question', 'Post Type Singular Name', 'text_domain' ),
- 'menu_name' => __( 'Multiple Choice Questions', 'text_domain' ),
- 'name_admin_bar' => __( 'Multiple Choice Question', 'text_domain' ),
- 'archives' => __( 'Multiple Choice Question Archives', 'text_domain' ),
- 'attributes' => __( 'Multiple Choice Question Attributes', 'text_domain' ),
- 'parent_item_colon' => __( 'Parent Multiple Choice Question:', 'text_domain' ),
- 'all_items' => __( 'All Multiple Choice Questions', 'text_domain' ),
- 'add_new_item' => __( 'Add New Multiple Choice Question', 'text_domain' ),
- 'add_new' => __( 'Add New', 'text_domain' ),
- 'new_item' => __( 'New Multiple Choice Question', 'text_domain' ),
- 'edit_item' => __( 'Edit Multiple Choice Question', 'text_domain' ),
- 'update_item' => __( 'Update Multiple Choice Question', 'text_domain' ),
- 'view_item' => __( 'View Multiple Choice Question', 'text_domain' ),
- 'view_items' => __( 'View Multiple Choice Questions', 'text_domain' ),
- 'search_items' => __( 'Search Multiple Choice Question', 'text_domain' ),
- 'not_found' => __( 'Not found', 'text_domain' ),
- 'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
- 'featured_image' => __( 'Featured Image', 'text_domain' ),
- 'set_featured_image' => __( 'Set featured image', 'text_domain' ),
- 'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
- 'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
- 'insert_into_item' => __( 'Insert into Multiple Choice Question', 'text_domain' ),
- 'uploaded_to_this_item' => __( 'Uploaded to this Multiple Choice Question', 'text_domain' ),
- 'items_list' => __( 'Multiple Choice Questions list', 'text_domain' ),
- 'items_list_navigation' => __( 'Multiple Choice Questions list navigation', 'text_domain' ),
- 'filter_items_list' => __( 'Filter Multiple Choice Questions list', 'text_domain' ),
- );
- $rewrite = array(
- 'slug' => 'course/%course%/quiz/%quiz%/question',
- 'with_front' => false,
- 'pages' => true,
- 'feeds' => false,
- );
- $args = array(
- 'label' => __( 'Multiple Choice Question', 'text_domain' ),
- 'description' => __( 'Post type used to house quizs', 'text_domain' ),
- 'labels' => $labels,
- 'supports' => array( 'title', 'editor', 'author', 'revisions', 'post-formats' ),
- 'hierarchical' => false,
- 'public' => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'menu_position' => 5,
- 'show_in_admin_bar' => true,
- 'show_in_nav_menus' => false,
- 'can_export' => true,
- 'has_archive' => false,
- 'exclude_from_search' => true,
- 'publicly_queryable' => true,
- 'query_var' => 'mc_quiz_question',
- 'rewrite' => $rewrite,
- 'capability_type' => 'post',
- );
- register_post_type( 'quiz_mc_post_type', $args );
- }
- add_action( 'init', 'quiz_mc_post_type', 0 );
- function quiz_mc_post_type_permalinks($permalink, $post, $leavename) {
- $post_id = $post->ID;
- if($post->post_type != 'quiz_mc_post_type' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
- return $permalink;
- }
- // GETS THE QUESTIONS PARENT WHICH IS A QUIZ
- $quiz = $post->post_parent;
- $actual_quiz = get_post( $quiz );
- $actual_quiz_name = $actual_quiz->post_name;
- // GETS THE QUIZ PARENT WHICH IS A COURSE
- $course = $actual_quiz->post_parent;
- $actual_course = get_post( $course );
- $actual_course_name = $actual_course->post_name;
- $needles = array("%course%", "%quiz%");
- $replacements = array($actual_course_name, $actual_quiz_name);
- $permalink = str_replace($needles, $replacements, $permalink);
- return $permalink;
- }
- add_filter('post_type_link', 'quiz_mc_post_type_permalinks', 10, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement