Advertisement
mbis

The Events Calendar - rewrite permalinks

Jan 10th, 2025 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.38 KB | None | 0 0
  1. /**
  2.  *
  3.  * Rewrite Events permalinks
  4.  *
  5.  */
  6. function bis_rewrite_recurring_event_permalinks( $post_link, $post, $leavename, $sample ) {
  7.     if ( ! empty( $post->post_type ) && $post->post_type == 'tribe_events' ) {
  8.         if( function_exists( 'tribe_is_recurring_event' ) && tribe_is_recurring_event( $post->ID ) ) {
  9.             $post_link = preg_replace( '/\/([\d]{4}-[\d]{2}-[\d]{2}\/?)$/', '-$1', $post_link );
  10.         }
  11.        
  12.         $post_type_object = get_post_type_object( $post->post_type );
  13.         $post_type_slug = ( isset( $post_type_object->rewrite ) && is_array( $post_type_object->rewrite ) && !empty( $post_type_object->rewrite['slug'] ) ) ? $post_type_object->rewrite['slug'] : '';
  14.            
  15.         if( ! empty( $post_type_slug ) ) {
  16.             $post_link = str_replace( "/{$post_type_slug}/", "/events/", $post_link );
  17.         }
  18.  
  19.         $post_link = user_trailingslashit( $post_link );
  20.     }
  21.  
  22.     return $post_link;
  23. }
  24. add_filter( 'post_type_link', 'bis_rewrite_recurring_event_permalinks', 100, 4 );
  25.  
  26. function pm_adjust_event_query( $query ) {
  27.     global $pm_query, $wp, $wpdb;
  28.  
  29.     // Do not run if custom permalink was detected
  30.     if ( ! empty( $pm_query['id'] ) ) {
  31.         return false;
  32.     }
  33.    
  34.     if ( strpos( $wp->request, 'events/' ) !== false ) {       
  35.         preg_match('/^events\/([^\/]+)(?:-([\d]{4}-[\d]{2}-[\d]{2}))?\/?$/U', $wp->request, $slug_parts );
  36.  
  37.         if ( ! empty( $slug_parts[1] ) ) {
  38.             // 2A. Check if the slug is assigned to any post item
  39.             $sql_query = "SELECT post_name, post_type FROM {$wpdb->posts} WHERE 1=1 AND post_name = %s AND post_type = 'tribe_events'";
  40.             $event_post = $wpdb->get_row($wpdb->prepare( $sql_query, array( $slug_parts[1] ) ) );
  41.            
  42.             if( ! empty( $event_post->post_name ) ) {
  43.                 $query = array(
  44.                     'post_type' => $event_post->post_type,
  45.                     $event_post->post_type => $event_post->post_name,
  46.                     'name' => $event_post->post_name,
  47.                     'do_not_redirect' => 1
  48.                 );
  49.                
  50.                 if ( ! empty( $slug_parts[2] ) ) {
  51.                     $query['eventDate']    = $slug_parts[2];
  52.                 }
  53.             }          
  54.         }
  55.     }
  56.  
  57.     return $query;
  58. }
  59. add_filter( 'request', 'pm_adjust_event_query', 2 );
  60.  
  61. function pm_redirect_old_event_urls() {
  62.     global $wp, $post;
  63.  
  64.     if ( is_singular( 'tribe_events' ) && ( strpos( $wp->request, 'event_detail/' ) !== false ) ) {
  65.         $product_permalink = get_permalink( $post->ID );
  66.  
  67.         wp_safe_redirect( $product_permalink, 301 );
  68.         exit();
  69.     }
  70. }
  71. add_action( 'template_redirect', 'pm_redirect_old_event_urls', 5 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement