Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function keha_add_rewrite_rules() {
- global $wp_rewrite;
- $taxonomies = get_taxonomies( array( '_builtin' => false ) );
- if( empty( $taxonomies ) ) {
- return false;
- }
- foreach ( $taxonomies as $taxonomy ) {
- $tax_obj = get_taxonomy( $taxonomy );
- $post_types = $tax_obj->object_type;
- foreach ( $post_types as $post_type ) {
- $post_type_obj = get_post_type_object( $post_type );
- $slug = $post_type_obj->rewrite['slug'];
- if( !$slug ) {
- $slug = $post_type;
- }
- if( is_string( $post_type_obj->has_archive ) ) {
- $slug = $post_type_obj->has_archive;
- };
- if ( isset( $tax_obj->rewrite['slug'] ) ) {
- $tax_pat = $tax_obj->rewrite['slug'];
- } else {
- $tax_pat = $taxonomy;
- }
- $tax = $taxonomy;
- add_rewrite_rule( $slug . '/' . $tax_pat . '/(.+?)/page/?([0-9]{1,})/?$', 'index.php?' . $tax . '=$matches[1]&paged=$matches[2]', 'top' );
- add_rewrite_rule( $slug . '/' . $tax_pat . '/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?' . $tax . '=$matches[1]&feed=$matches[2]', 'top' );
- add_rewrite_rule( $slug . '/' . $tax_pat . '/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?' . $tax . '=$matches[1]&feed=$matches[2]', 'top' );
- add_rewrite_rule( $slug . '/' . $tax_pat . '/(.+?)/?$', 'index.php?' . $tax . '=$matches[1]', 'top' );
- add_rewrite_rule( $tax_pat . '/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?' . $tax . '=$matches[1]&year=$matches[2]&monthnum=$matches[3]&post_type=' . $post_type, 'top' );
- add_rewrite_rule( $tax_pat . '/(.+?)/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?' . $tax . '=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]&post_type=' . $post_type, 'top' );
- add_rewrite_rule( $slug . '/' . $tax_pat . '/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?' . $tax . '=$matches[1]&year=$matches[2]&monthnum=$matches[3]&post_type=' . $post_type, 'top' );
- add_rewrite_rule( $slug . '/' . $tax_pat . '/(.+?)/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?' . $tax . '=$matches[1]&year=$matches[2]&monthnum=$matches[3]&paged=$matches[4]&post_type=' . $post_type, 'top' );
- }
- }
- }
- add_action( 'wp_loaded', 'keha_add_rewrite_rules' );
- function keha_post_type_link( $post_link, $post, $leavename ) {
- global $wp_rewrite;
- $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
- if( $draft_or_pending and !$leavename )
- return $post_link;
- $post_type = $post->post_type;
- $permalink = $wp_rewrite->get_extra_permastruct( $post_type );
- $permalink = str_replace( '%post_id%', $post->ID, $permalink );
- $permalink = str_replace( '%'.$post_type.'_slug%', get_post_type_object( $post_type )->rewrite['slug'], $permalink );
- $parents_dirs = "";
- if( !$leavename ){
- $postId = $post->ID;
- while ( $parent = get_post( $postId )->post_parent) {
- $parents_dirs = get_post( $parent )->post_name."/".$parents_dirs;
- $postId = $parent;
- }
- }
- $permalink = str_replace( '%'.$post_type.'%', $parents_dirs.'%'.$post_type.'%', $permalink );
- if( !$leavename ){
- $permalink = str_replace( '%'.$post_type.'%', $post->post_name, $permalink );
- }
- $taxonomies = get_taxonomies( array( 'show_ui' => true ),'objects' );
- // %taxonomy% -> parent/child
- foreach ( $taxonomies as $taxonomy => $objects ) {
- if ( strpos( $permalink, "%$taxonomy%" ) !== false ) {
- $terms = wp_get_post_terms( $post->ID, $taxonomy, array('orderby' => 'term_id'));
- if ( $terms && count( $terms ) > 1 ) {
- if ( reset( $terms )->parent == 0 ) {
- $keys = array_keys( $terms );
- $term = $terms[$keys[1]]->slug;
- if ( $terms[ $keys[0] ]->term_id == $terms[ $keys[1] ]->parent ) {
- $term = keha_get_taxonomy_parents( $terms[ $keys[1] ]->parent, $taxonomy, false, '/', true ) . $term;
- }
- } else {
- $keys = array_keys( $terms );
- $term = $terms[ $keys[0] ]->slug;
- if ( $terms[ $keys[1] ]->term_id == $terms[ $keys[0] ]->parent ) {
- $term = keha_get_taxonomy_parents( $terms[ $keys[0] ]->parent, $taxonomy, false, '/', true ) . $term;
- }
- }
- } elseif ( $terms ) {
- $term_obj = array_shift( $terms );
- $term = $term_obj->slug;
- if( isset( $term_obj->parent ) and $term_obj->parent != 0 ) {
- $term = keha_get_taxonomy_parents( $term_obj->parent, $taxonomy, false, '/', true ) . $term;
- }
- }
- if( isset( $term ) ) {
- $permalink = str_replace( "%$taxonomy%", $term, $permalink );
- }
- }
- }
- return $permalink;
- }
- add_filter('post_type_link', 'keha_post_type_link', 10, 3);
- function keha_term_link( $termlink, $term, $taxonomy ) {
- $taxonomy = get_taxonomy( $taxonomy );
- if( $taxonomy->_builtin )
- return $termlink;
- if( empty( $taxonomy ) )
- return $termlink;
- $wp_home = rtrim( home_url(), '/' );
- $post_type = $taxonomy->object_type[0];
- $slug = get_post_type_object( $post_type )->rewrite['slug'];
- $with_front = get_post_type_object( $post_type )->rewrite['with_front'];
- $str = rtrim( preg_replace( "/%[a-z_]*%/", "" , get_option( "permalink_structure" ) ) ,'/' );
- $termlink = str_replace( $str . "/", "/", $termlink );
- if( $with_front === false ) {
- $str = "";
- }
- $slug = $str . "/" . $slug;
- $termlink = str_replace( $wp_home, $wp_home . $slug, $termlink );
- $termlink = str_replace( $term->slug.'/', keha_get_taxonomy_parents( $term->term_id, $taxonomy->name, false, '/', true ), $termlink );
- return $termlink;
- }
- add_filter( 'term_link', 'keha_term_link', 10, 3 );
- function keha_get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) {
- $chain = '';
- $parent = get_term( $id, $taxonomy, OBJECT, 'raw');
- if ( is_wp_error( $parent ) ) {
- return $parent;
- }
- if ( $nicename ){
- $name = $parent->slug;
- }else {
- $name = $parent->name;
- }
- if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
- $visited[] = $parent->parent;
- $chain .= keha_get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
- }
- if ( $link ) {
- $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
- }else {
- $chain .= $name.$separator;
- }
- return $chain;
- }
- function keha_register_photo_tax_album() {
- $labels = array(
- 'name' => _x( 'Albums', 'taxonomy general name', 'blank' ),
- 'singular_name' => _x( 'Album', 'taxonomy singular name', 'blank' ),
- 'search_items' => __( 'Search Albums', 'blank' ),
- 'all_items' => __( 'All Albums', 'blank' ),
- 'parent_item' => __( 'Parent Album', 'blank' ),
- 'parent_item_colon' => __( 'Parent Album:', 'blank' ),
- 'edit_item' => __( 'Edit Album', 'blank' ),
- 'update_item' => __( 'Update Album', 'blank' ),
- 'add_new_item' => __( 'Add New Album', 'blank' ),
- 'new_item_name' => __( 'New Album Name', 'blank' ),
- 'menu_name' => __( 'Albums', 'blank' ),
- );
- $rewrite = array(
- 'slug' => 'album',
- 'with_front' => false,
- 'hierarchical' => true,
- );
- $args = array(
- 'hierarchical' => true,
- 'labels' => $labels,
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => $rewrite
- );
- register_taxonomy( 'album', array('photo'), $args );
- }
- add_action( 'init', 'keha_register_photo_tax_album' );
- function keha_register_cpt_photo() {
- $labels = array(
- 'name' => __( 'Photos', 'blank' ),
- 'singular_name' => _x( 'Photo', 'Photo post type singular', 'blank' ),
- 'add_new' => __( 'Add New', 'blank' ),
- 'add_new_item' => __( 'Add New Photo', 'blank' ),
- 'edit_item' => __( 'Edit Photo', 'blank' ),
- 'new_item' => __( 'New Photo', 'blank' ),
- 'all_items' => __( 'All Photos', 'blank' ),
- 'view_item' => __( 'View Photo', 'blank' ),
- 'search_items' => __( 'Search Photos', 'blank' ),
- 'not_found' => __( 'No photos found', 'blank' ),
- 'not_found_in_trash' => __( 'No photos found in Trash', 'blank' ),
- 'parent_item_colon' => '',
- 'menu_name' => __( 'Photos', 'blank' )
- );
- $args = array(
- 'labels' => $labels,
- 'exclude_from_search' => false,
- 'publicly_queryable' => true,
- 'show_ui' => true,
- 'show_in_nav_menus' => true,
- 'show_in_menu' => true,
- 'show_in_admin_bar' => true,
- 'menu_position' => 20,
- 'menu_icon' => null,
- 'capability_type' => 'post',
- 'hierarchical' => false,
- 'public' => true,
- 'supports' => array( 'title', 'editor', 'thumbnail' ),
- 'taxonomies' => array( 'album' ),
- 'rewrite' => array( 'slug' =>'photos/%album%' ),
- 'has_archive' => true,
- 'query_var' => true,
- 'can_export' => true
- );
- register_post_type( 'photo', $args );
- }
- add_action( 'init', 'keha_register_cpt_photo' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement