Advertisement
keha76

WordPress Hierarchical Permalinks

Sep 17th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.09 KB | None | 0 0
  1. <?php
  2.  
  3. function keha_add_rewrite_rules() {
  4.  
  5.     global $wp_rewrite;
  6.  
  7.     $taxonomies = get_taxonomies( array( '_builtin' => false ) );
  8.  
  9.     if( empty( $taxonomies ) ) {
  10.         return false;
  11.     }
  12.  
  13.     foreach ( $taxonomies as $taxonomy ) {
  14.  
  15.         $tax_obj = get_taxonomy( $taxonomy );
  16.         $post_types = $tax_obj->object_type;
  17.  
  18.         foreach ( $post_types as $post_type ) {
  19.  
  20.             $post_type_obj = get_post_type_object( $post_type );
  21.            
  22.  
  23.             $slug = $post_type_obj->rewrite['slug'];
  24.            
  25.             if( !$slug ) {
  26.                 $slug = $post_type;
  27.             }
  28.  
  29.             if( is_string( $post_type_obj->has_archive ) ) {
  30.                 $slug = $post_type_obj->has_archive;
  31.             };
  32.  
  33.  
  34.             if ( isset( $tax_obj->rewrite['slug'] ) ) {
  35.  
  36.                 $tax_pat = $tax_obj->rewrite['slug'];
  37.  
  38.             } else {
  39.  
  40.                 $tax_pat = $taxonomy;
  41.  
  42.             }
  43.            
  44.             $tax = $taxonomy;
  45.  
  46.             add_rewrite_rule( $slug . '/' . $tax_pat . '/(.+?)/page/?([0-9]{1,})/?$', 'index.php?' . $tax . '=$matches[1]&paged=$matches[2]', 'top' );
  47.             add_rewrite_rule( $slug . '/' . $tax_pat . '/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?' . $tax . '=$matches[1]&feed=$matches[2]', 'top' );
  48.             add_rewrite_rule( $slug . '/' . $tax_pat . '/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?' . $tax . '=$matches[1]&feed=$matches[2]', 'top' );
  49.             add_rewrite_rule( $slug . '/' . $tax_pat . '/(.+?)/?$', 'index.php?' . $tax . '=$matches[1]', 'top' );
  50.             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' );
  51.             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' );
  52.             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' );
  53.             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' );
  54.  
  55.         }
  56.     }
  57. }
  58. add_action( 'wp_loaded', 'keha_add_rewrite_rules' );
  59.  
  60. function keha_post_type_link( $post_link, $post, $leavename ) {
  61.  
  62.     global $wp_rewrite;
  63.  
  64.     $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
  65.     if( $draft_or_pending and !$leavename )
  66.         return $post_link;
  67.  
  68.     $post_type = $post->post_type;
  69.     $permalink = $wp_rewrite->get_extra_permastruct( $post_type );
  70.     $permalink = str_replace( '%post_id%', $post->ID, $permalink );
  71.     $permalink = str_replace( '%'.$post_type.'_slug%', get_post_type_object( $post_type )->rewrite['slug'], $permalink );
  72.    
  73.     $parents_dirs = "";
  74.    
  75.     if( !$leavename ){
  76.         $postId = $post->ID;
  77.         while ( $parent = get_post( $postId )->post_parent) {
  78.             $parents_dirs = get_post( $parent )->post_name."/".$parents_dirs;
  79.             $postId = $parent;
  80.         }
  81.     }
  82.  
  83.     $permalink = str_replace( '%'.$post_type.'%', $parents_dirs.'%'.$post_type.'%', $permalink );
  84.    
  85.     if( !$leavename ){
  86.         $permalink = str_replace( '%'.$post_type.'%', $post->post_name, $permalink );
  87.     }
  88.            
  89.     $taxonomies = get_taxonomies( array( 'show_ui' => true ),'objects' );
  90.    
  91.     // %taxonomy% -> parent/child
  92.     foreach ( $taxonomies as $taxonomy => $objects ) {
  93.  
  94.         if ( strpos( $permalink, "%$taxonomy%" ) !== false ) {
  95.  
  96.             $terms = wp_get_post_terms( $post->ID, $taxonomy, array('orderby' => 'term_id'));
  97.            
  98.             if ( $terms && count( $terms ) > 1 ) {
  99.                 if ( reset( $terms )->parent == 0 ) {
  100.  
  101.                     $keys = array_keys( $terms );
  102.                     $term = $terms[$keys[1]]->slug;
  103.  
  104.                     if ( $terms[ $keys[0] ]->term_id == $terms[ $keys[1] ]->parent ) {
  105.                         $term = keha_get_taxonomy_parents( $terms[ $keys[1] ]->parent, $taxonomy, false, '/', true ) . $term;
  106.                     }
  107.  
  108.                 } else {
  109.  
  110.                     $keys = array_keys( $terms );
  111.                     $term = $terms[ $keys[0] ]->slug;
  112.  
  113.                     if ( $terms[ $keys[1] ]->term_id == $terms[ $keys[0] ]->parent ) {
  114.                         $term = keha_get_taxonomy_parents( $terms[ $keys[0] ]->parent, $taxonomy, false, '/', true ) . $term;
  115.                     }
  116.  
  117.                 }
  118.  
  119.             } elseif ( $terms ) {
  120.  
  121.                 $term_obj = array_shift( $terms );
  122.                 $term = $term_obj->slug;
  123.  
  124.                 if( isset( $term_obj->parent ) and $term_obj->parent != 0 ) {
  125.                     $term = keha_get_taxonomy_parents( $term_obj->parent, $taxonomy, false, '/', true ) . $term;
  126.                 }
  127.             }
  128.  
  129.             if( isset( $term ) ) {
  130.                 $permalink = str_replace( "%$taxonomy%", $term, $permalink );
  131.             }
  132.         }
  133.     }
  134.     return $permalink;
  135. }
  136. add_filter('post_type_link', 'keha_post_type_link', 10, 3);
  137.  
  138. function keha_term_link( $termlink, $term, $taxonomy ) {
  139.    
  140.     $taxonomy = get_taxonomy( $taxonomy );
  141.  
  142.     if( $taxonomy->_builtin )
  143.         return $termlink;
  144.  
  145.     if( empty( $taxonomy ) )
  146.         return $termlink;
  147.  
  148.     $wp_home = rtrim( home_url(), '/' );
  149.  
  150.     $post_type = $taxonomy->object_type[0];
  151.     $slug = get_post_type_object( $post_type )->rewrite['slug'];
  152.     $with_front = get_post_type_object( $post_type )->rewrite['with_front'];
  153.  
  154.     $str = rtrim( preg_replace( "/%[a-z_]*%/", "" , get_option( "permalink_structure" ) ) ,'/' );
  155.     $termlink = str_replace( $str . "/", "/", $termlink );
  156.  
  157.     if( $with_front === false ) {
  158.         $str = "";
  159.     }
  160.     $slug = $str . "/" . $slug;
  161.  
  162.     $termlink = str_replace( $wp_home, $wp_home . $slug, $termlink );
  163.     $termlink = str_replace( $term->slug.'/', keha_get_taxonomy_parents( $term->term_id, $taxonomy->name, false, '/', true ), $termlink );
  164.  
  165.     return $termlink;
  166. }
  167. add_filter( 'term_link', 'keha_term_link', 10, 3 );
  168.  
  169. function keha_get_taxonomy_parents( $id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false, $visited = array() ) {
  170.     $chain = '';
  171.     $parent = get_term( $id, $taxonomy, OBJECT, 'raw');
  172.     if ( is_wp_error( $parent ) ) {
  173.         return $parent;
  174.     }
  175.  
  176.     if ( $nicename ){
  177.         $name = $parent->slug;
  178.     }else {
  179.         $name = $parent->name;
  180.     }
  181.  
  182.     if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
  183.         $visited[] = $parent->parent;
  184.         $chain .= keha_get_taxonomy_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
  185.     }
  186.  
  187.     if ( $link ) {
  188.         $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
  189.     }else {
  190.         $chain .= $name.$separator;
  191.     }
  192.     return $chain;
  193. }
  194.  
  195. function keha_register_photo_tax_album() {
  196.        
  197.     $labels = array(
  198.         'name'              => _x( 'Albums', 'taxonomy general name', 'blank' ),
  199.         'singular_name'     => _x( 'Album', 'taxonomy singular name', 'blank' ),
  200.         'search_items'      => __( 'Search Albums', 'blank' ),
  201.         'all_items'         => __( 'All Albums', 'blank' ),
  202.         'parent_item'       => __( 'Parent Album', 'blank' ),
  203.         'parent_item_colon' => __( 'Parent Album:', 'blank' ),
  204.         'edit_item'         => __( 'Edit Album', 'blank' ),
  205.         'update_item'       => __( 'Update Album', 'blank' ),
  206.         'add_new_item'      => __( 'Add New Album', 'blank' ),
  207.         'new_item_name'     => __( 'New Album Name', 'blank' ),
  208.         'menu_name'         => __( 'Albums', 'blank' ),
  209.     );  
  210.  
  211.     $rewrite = array(
  212.         'slug' => 'album',
  213.         'with_front' => false,
  214.         'hierarchical' => true,
  215.     );
  216.  
  217.     $args = array(
  218.         'hierarchical' => true,
  219.         'labels'       => $labels,
  220.         'show_ui'      => true,
  221.         'query_var'    => true,
  222.         'rewrite'      => $rewrite
  223.     );
  224.  
  225.     register_taxonomy( 'album', array('photo'), $args );
  226. }
  227. add_action( 'init', 'keha_register_photo_tax_album' );
  228.  
  229. function keha_register_cpt_photo() {
  230.  
  231.     $labels = array(
  232.         'name'               => __( 'Photos', 'blank' ),
  233.         'singular_name'      => _x( 'Photo', 'Photo post type singular', 'blank' ),
  234.         'add_new'            => __( 'Add New', 'blank' ),
  235.         'add_new_item'       => __( 'Add New Photo', 'blank' ),
  236.         'edit_item'          => __( 'Edit Photo', 'blank' ),
  237.         'new_item'           => __( 'New Photo', 'blank' ),
  238.         'all_items'          => __( 'All Photos', 'blank' ),
  239.         'view_item'          => __( 'View Photo', 'blank' ),
  240.         'search_items'       => __( 'Search Photos', 'blank' ),
  241.         'not_found'          => __( 'No photos found', 'blank' ),
  242.         'not_found_in_trash' => __( 'No photos found in Trash', 'blank' ),
  243.         'parent_item_colon'  => '',
  244.         'menu_name'          => __( 'Photos', 'blank' )
  245.     );
  246.  
  247.     $args = array(
  248.         'labels'               => $labels,
  249.         'exclude_from_search'  => false,
  250.         'publicly_queryable'   => true,
  251.         'show_ui'              => true,
  252.         'show_in_nav_menus'    => true,
  253.         'show_in_menu'         => true,
  254.         'show_in_admin_bar'    => true,
  255.         'menu_position'        => 20,
  256.         'menu_icon'            => null,
  257.         'capability_type'      => 'post',
  258.         'hierarchical'         => false,
  259.         'public'               => true,
  260.         'supports'             => array( 'title', 'editor', 'thumbnail' ),
  261.         'taxonomies'           => array( 'album' ),
  262.         'rewrite'              => array( 'slug' =>'photos/%album%' ),
  263.         'has_archive'          => true,
  264.         'query_var'            => true,
  265.         'can_export'           => true
  266.     );
  267.  
  268.     register_post_type( 'photo', $args );
  269. }
  270. add_action( 'init', 'keha_register_cpt_photo' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement