Advertisement
Visnetje

WP rewrite permalinks for custom post type

Sep 14th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.38 KB | None | 0 0
  1. /**
  2.  * Get 'Training' custom post type meta info group variables.
  3.  *
  4.  * @uses get_post_meta()
  5.  * @uses esc_attr()
  6.  */
  7. function tr2014_get_training_first_date( $post ) {
  8.     $options = get_post_meta( $post->ID, '_tr_training_option_group', true );
  9.  
  10.     $date_start = false;
  11.     foreach ( (array) $options as $key => $option ) {
  12.         if ( !empty( $option['_tr_training_date_start'] ) ) {
  13.             $date_start = strtotime( $option['_tr_training_date_start'] );
  14.             break;
  15.         }
  16.     }
  17.  
  18.     return $date_start;
  19. }
  20.  
  21. /**
  22.  * Create custom post types for 'Training'
  23.  */
  24. function tr2014_create_post_types() {
  25.     register_post_type( 'tr_training',
  26.         array(
  27.             'labels' => array(
  28.                 'name'                  => __( 'Trainingen', 'tr2014' ),
  29.                 'singular_name'         => __( 'Training', 'tr2014' ),
  30.                 'add_new'               => __( 'Nieuwe training', 'tr2014' ),
  31.                 'add_new_item'          => __( 'Nieuwe training toevoegen', 'tr2014' ),
  32.                 'edit_item'             => __( 'Wijzig training', 'tr2014' ),
  33.                 'new_item'              => __( 'Nieuwe training toevoegen', 'tr2014' ),
  34.                 'view_item'             => __( 'Training bekijken', 'tr2014' ),
  35.                 'search_items'          => __( 'Trainingsaanbod doorzoeken', 'tr2014' ),
  36.                 'not_found'             => __( 'Geen trainingen gevonden.', 'tr2014' ),
  37.                 'not_found_in_trash'    => __( 'Geen trainingen gevonden in Prullenbak.', 'tr2014' ),
  38.                 'all_items'             => __( 'Alle trainingen', 'tr2014' )
  39.             ),
  40.             'public'                => true,
  41.             'query_var'             => true,
  42.             'rewrite'               => false,
  43.             'menu_position'         => 25,
  44.             'menu_icon'             => 'dashicons-groups',
  45.             'capability_type'       => 'page',
  46.             'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom_fields' ),
  47.         )
  48.     );
  49. }
  50. add_action( 'init', 'tr2014_create_post_types' );
  51.  
  52.  
  53. /**
  54.  * Properly replace custom post type(s) permalinks.
  55.  */
  56. function tr2014_post_type_link( $url, $post, $leavename = false ) {
  57.  
  58.     if ( !is_single() && get_post_type( $post ) == 'tr_training' ) {
  59.         $new_url = $url;
  60.  
  61.         $re_search = array(
  62.             '%startdate%',
  63.             $leavename ? '' : '%postname%',
  64.             '%post_id%',
  65.             $leavename ? '' : '%pagename%',
  66.         );
  67.  
  68.         $date_start = tr2014_get_training_first_date( $post );
  69.  
  70.         if ( !empty( $url ) && !in_array( $post->post_status, array('draft', 'pending', 'auto-draft') ) ) {
  71.             if ( $date_start ) {
  72.                 // Append start date to permalink, if it exists
  73.                 $new_url .= '/%startdate%';
  74.             }
  75.  
  76.             // Replace %startdate% with formatted date, e.g. "2014-12-23"
  77.             $re_replace = array(
  78.                 ( $date_start ? date( 'Y-m-d', $date_start ) : ''),
  79.                 $post->post_name,
  80.                 $post->ID,
  81.                 $post->post_name,
  82.             );
  83.             $new_url = str_replace( $re_search, $re_replace, $new_url );
  84.         } else {
  85.             // Not using rewrite/permalink option
  86.         }
  87.     }
  88.  
  89.     return $new_url;
  90. }
  91. add_filter( 'post_type_link', 'tr2014_post_type_link', 10, 2 );
  92.  
  93.  
  94. /**
  95.  * Add support for %startdate% tag in permalinks.
  96.  */
  97. function tr2014_rewrite_tags() {
  98.     //add_rewrite_tag( '%startdate%', '([0-9]{4}-[0-9]{2}-[0-9]{2})$', 'startdate=' );
  99.     //add_permastruct( 'tr_training', '/trainings/%postname%/%startdate%', false );
  100.     add_rewrite_tag( '%training%', '([^/]+)(/.*)$', 'tr_training=' );
  101.     add_rewrite_tag( '%startdate%', '([^/]+)(/.*)$', 'tr_training=' );
  102.     add_permastruct( 'tr_training', '/trainings/%training%/%startdate%', false );
  103.     //add_rewrite_tag( '%postname%', '([^/]+)', 'tr_training=' );
  104.     //add_permastruct( 'tr_training', '/trainings/%postname%', false );
  105. }
  106. add_action( 'init', 'tr2014_rewrite_tags', 10, 0 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement