|
<?php |
|
/** |
|
* Plugin Name: SEO Fix for The Events Calendar |
|
* Plugin URI: https://github.com/jdevalk/seo-fix-tec |
|
* Description: A plugin to fix and enhance The Events Calendar functionality |
|
* Version: 1.0.0 |
|
* Author: |
|
* Author URI: https://joost.blog/ |
|
* License: GPL v2 or later |
|
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
|
*/ |
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
exit; // Exit if accessed directly |
|
} |
|
|
|
// Disable caching as it breaks the noindex functionality. |
|
add_filter( 'tribe_events_views_v2_should_cache_html', '__return_false' ); |
|
|
|
add_filter( 'tec_events_seo_robots_meta_include', function( $do_noindex ) { |
|
// If the noindex is enabled, add the noindex to the WordPress core robots meta tag (which SEO plugins also use). |
|
if ( $do_noindex ) { |
|
// Add noindex to the WordPress robots meta tag. |
|
add_filter( 'wp_robots', function( $robots ) { |
|
$robots['noindex'] = true; |
|
|
|
return $robots; |
|
}, 10, 1 ); |
|
|
|
// Remove the Yoast SEO canonical URL from the page, as that potentially breaks the noindex functionality. |
|
// Yoast should not be outputting a canonical URL in this case, but it seems to do so anyway. |
|
add_filter( 'wpseo_canonical', '__return_false' ); |
|
|
|
// Return false to prevent the default behavior of adding a second robots meta tag. |
|
return false; |
|
} |
|
return $do_noindex; |
|
}, 999, 1 ); |
|
|
|
add_filter( 'send_headers', function() { |
|
|
|
global $wp_query; |
|
if ( isset( $wp_query->query['post_type'] ) && $wp_query->query['post_type'] === 'tribe_events' && isset( $wp_query->query['eventDisplay'] ) ) { |
|
|
|
if ( $wp_query->query['eventDisplay'] === 'month' ) { |
|
// If the month view is disabled, always return a 404. |
|
$raw_options = Tribe__Settings_Manager::get_options(); |
|
if ( ! in_array( 'month', $raw_options['tribeEnableViews'] ) ) { |
|
$wp_query->set_404(); |
|
return; |
|
} |
|
|
|
// If the month view is enabled, but the earliest event date is after the current month, return 404. |
|
if ( tribe_events_earliest_date( 'Y-m' ) > $wp_query->query['eventDate'] ) { |
|
$wp_query->set_404(); |
|
return; |
|
} |
|
|
|
// If the month view is enabled, but the latest event date is before the current month, return 404. |
|
if ( tribe_events_latest_date( 'Y-m' ) < $wp_query->query['eventDate'] ) { |
|
$wp_query->set_404(); |
|
return; |
|
} |
|
} |
|
|
|
if ( $wp_query->query['eventDisplay'] === 'day' ) { |
|
// If the day view is disabled, always return a 404. |
|
$raw_options = Tribe__Settings_Manager::get_options(); |
|
if ( ! in_array( 'day', $raw_options['tribeEnableViews'] ) ) { |
|
$wp_query->set_404(); |
|
return; |
|
} |
|
|
|
// If the day view is enabled, but the currently shown date is before the earliest event date, return 404. |
|
if ( strtotime( tribe_events_earliest_date( 'Y-m-d' ) ) > strtotime( $wp_query->query['eventDate'] ) ) { |
|
$wp_query->set_404(); |
|
return; |
|
} |
|
|
|
// If the day view is enabled, but the currently shown date is after the latest event date, return 404. |
|
if ( strtotime( tribe_events_latest_date( 'Y-m-d' ) ) < strtotime( $wp_query->query['eventDate'] ) ) { |
|
$wp_query->set_404(); |
|
return; |
|
} |
|
} |
|
} |
|
}); |