Skip to content

Instantly share code, notes, and snippets.

@mboynes
Created February 28, 2014 04:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mboynes/9264987 to your computer and use it in GitHub Desktop.
Save mboynes/9264987 to your computer and use it in GitHub Desktop.
WPSE 135707
<?php
function add_video_post_type() {
$labels = array(
'name' => _x( 'Videos', 'Post Type General Name', 'kibble' ),
'singular_name' => _x( 'Video', 'Post Type Singular Name', 'kibble' ),
'menu_name' => __( 'Videos', 'kibble' ),
'parent_item_colon' => __( 'Parent Item:', 'kibble' ),
'all_items' => __( 'All Items', 'kibble' ),
'view_item' => __( 'View Item', 'kibble' ),
'add_new_item' => __( 'Add New Item', 'kibble' ),
'add_new' => __( 'Add New', 'kibble' ),
'edit_item' => __( 'Edit Item', 'kibble' ),
'update_item' => __( 'Update Item', 'kibble' ),
'search_items' => __( 'Search Item', 'kibble' ),
'not_found' => __( 'Not found', 'kibble' ),
'not_found_in_trash' => __( 'Not found in Trash', 'kibble' ),
);
$rewrite = array(
'slug' => '%video_genre%',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'video_type', 'kibble' ),
'description' => __( 'Video Posts', 'kibble' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', ),
'taxonomies' => array( 'video_genre' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-video',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'video_type', $args );
}
add_action( 'init', 'add_video_post_type', 0 );
function custom_video_genre() {
$labels = array(
'name' => _x( 'Video Categories', 'Taxonomy General Name', 'kibble' ),
'singular_name' => _x( 'Video Category', 'Taxonomy Singular Name', 'kibble' ),
'menu_name' => __( 'Categories', 'kibble' ),
'all_items' => __( 'All Items', 'kibble' ),
'parent_item' => __( 'Parent Item', 'kibble' ),
'parent_item_colon' => __( 'Parent Item:', 'kibble' ),
'new_item_name' => __( 'New Item Name', 'kibble' ),
'add_new_item' => __( 'Add New Item', 'kibble' ),
'edit_item' => __( 'Edit Item', 'kibble' ),
'update_item' => __( 'Update Item', 'kibble' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'kibble' ),
'search_items' => __( 'Search Items', 'kibble' ),
'add_or_remove_items' => __( 'Add or remove items', 'kibble' ),
'choose_from_most_used' => __( 'Choose from the most used items', 'kibble' ),
'not_found' => __( 'Not Found', 'kibble' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'video_genre', 'video_type', $args );
}
add_action( 'init', 'custom_video_genre', 0 );
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
function filter_post_type_link( $link, $post ) {
if ( strpos( $link, '%video_genre%' ) === false )
return $link;
$terms = wp_get_object_terms( $post->ID, 'video_genre' );
if ( current( $terms ) ) :
$new_parent = current( $terms );
foreach ( $terms as $term ) {
if ( $term->parent == 0 ) {
$new_parent = $term;
break;
}
}
$new_parent = $new_parent ? $new_parent : $terms[0];
$category_string = $new_parent->slug;
$category_string = trim( $category_string, '/' );
endif;
// set a default
$category_string = ( isset( $category_string ) && $category_string ) ? $category_string : 'uncategorized';
$link = str_replace( '%video_genre%', $category_string, $link );
return $link;
}
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
function wpse_135707_video_type_rewrite_rules( $rules ) {
$GLOBALS['video_type_rewrite_rules'] = $rules;
return array();
}
add_filter( 'video_type_rewrite_rules', 'wpse_135707_video_type_rewrite_rules' );
function wpse_135707_add_video_type_to_rewrite_rules_array( $rules ) {
return array_merge( $rules, $GLOBALS['video_type_rewrite_rules'] );
}
add_filter( 'rewrite_rules_array', 'wpse_135707_add_video_type_to_rewrite_rules_array' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment