cliffordp
11/9/2016 - 3:03 AM

The Events Calendar / PRO: Redirect tribe_events archive pages to custom page (MUST ENTER IT YOURSELF)

The Events Calendar / PRO: Redirect tribe_events archive pages to custom page (MUST ENTER IT YOURSELF)

<?php

/**
  * The Events Calendar / PRO:
  * Redirect tribe_events archive pages to custom page (MUST ENTER IT YOURSELF, BELOW)
  * Does not carry-forward query string parameters
  *
  * From https://gist.github.com/cliffordp/acfde15af9cea91af4fbec168182fd1d
  *
  * @link https://theeventscalendar.com/knowledgebase/embedding-calendar-views-tribe_events-shortcode/
  */
add_action( 'template_redirect', 'tribe_events_redirect_custom_page' );
function tribe_events_redirect_custom_page(){
	//
	// BEGIN SETUP
	//
	
	// a relative URI like '/custom-events/' (probably worse for SEO) or a full URL beginning with http://... (probably better for SEO)
	$redirect_to = ''; // e.g. 'http://wpshindig.com/my-custom-page-link/'
	
	// 301 is permanent. 302 is temporary.
	// https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection
	$redirection_code = 301;
	
	
	//
	// END SETUP, BEGIN LOGIC
	//
	if ( ! class_exists( 'Tribe__Events__Main' ) ) {
		return false;
	}
	
	$tecmain = Tribe__Events__Main::instance();
	
	$redirect_to = esc_url_raw( $redirect_to );
	
	if ( empty( $redirect_to ) ) {
		return false;
	}
	
	if ( is_main_query() && is_post_type_archive( $tecmain::POSTTYPE ) ) {
		wp_redirect( $redirect_to, $redirection_code );
		exit;
	}
	
	return null;
}