Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Rewrite Events permalinks
- *
- */
- function bis_rewrite_recurring_event_permalinks( $post_link, $post, $leavename, $sample ) {
- if ( ! empty( $post->post_type ) && $post->post_type == 'tribe_events' ) {
- if( function_exists( 'tribe_is_recurring_event' ) && tribe_is_recurring_event( $post->ID ) ) {
- $post_link = preg_replace( '/\/([\d]{4}-[\d]{2}-[\d]{2}\/?)$/', '-$1', $post_link );
- }
- $post_type_object = get_post_type_object( $post->post_type );
- $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'] : '';
- if( ! empty( $post_type_slug ) ) {
- $post_link = str_replace( "/{$post_type_slug}/", "/events/", $post_link );
- }
- $post_link = user_trailingslashit( $post_link );
- }
- return $post_link;
- }
- add_filter( 'post_type_link', 'bis_rewrite_recurring_event_permalinks', 100, 4 );
- function pm_adjust_event_query( $query ) {
- global $pm_query, $wp, $wpdb;
- // Do not run if custom permalink was detected
- if ( ! empty( $pm_query['id'] ) ) {
- return false;
- }
- if ( strpos( $wp->request, 'events/' ) !== false ) {
- preg_match('/^events\/([^\/]+)(?:-([\d]{4}-[\d]{2}-[\d]{2}))?\/?$/U', $wp->request, $slug_parts );
- if ( ! empty( $slug_parts[1] ) ) {
- // 2A. Check if the slug is assigned to any post item
- $sql_query = "SELECT post_name, post_type FROM {$wpdb->posts} WHERE 1=1 AND post_name = %s AND post_type = 'tribe_events'";
- $event_post = $wpdb->get_row($wpdb->prepare( $sql_query, array( $slug_parts[1] ) ) );
- if( ! empty( $event_post->post_name ) ) {
- $query = array(
- 'post_type' => $event_post->post_type,
- $event_post->post_type => $event_post->post_name,
- 'name' => $event_post->post_name,
- 'do_not_redirect' => 1
- );
- if ( ! empty( $slug_parts[2] ) ) {
- $query['eventDate'] = $slug_parts[2];
- }
- }
- }
- }
- return $query;
- }
- add_filter( 'request', 'pm_adjust_event_query', 2 );
- function pm_redirect_old_event_urls() {
- global $wp, $post;
- if ( is_singular( 'tribe_events' ) && ( strpos( $wp->request, 'event_detail/' ) !== false ) ) {
- $product_permalink = get_permalink( $post->ID );
- wp_safe_redirect( $product_permalink, 301 );
- exit();
- }
- }
- add_action( 'template_redirect', 'pm_redirect_old_event_urls', 5 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement