Support » Plugin: noindex Past Events » Proposed Patch: Add Yoast SEO integration

  • Hi,

    I use Yoast SEO on my site along with The Events Calendar and Algolia Search. I have my Algolia search index filter out results based on the Yoast SEO index setting for a given post (via the function shown on https://github.com/algolia/algoliasearch-wordpress/blob/develop/docs/src/indexing-flow.md). This plugin is great, but it currently just adds a noindex tag onto the page without interacting with anything else so this leaves my Algolia Search indexing these past events as well as Yoast SEO not knowing these pages are set to noindex.

    I’ve proposed a patch which keeps the existing method if Yoast SEO isn’t enabled while opting to use Yoast SEO for noindex if it is enabled. Also, I swapped out time() for current_time() so it references WordPress’ local time for determining whether an event is passed.

    Here’s the full noindex-past-events.php file after my patch:

    
    <?php
    /*
    Plugin Name: noindex Past Events
    Plugin URI:  http://room34.com
    Description: Automatically add a "noindex" meta tag to past events' detail pages to prevent them from appearing in search engines.
    Version:     1.1.0.1
    Author:      Room 34 Creative Services, LLC
    Author URI:  http://room34.com
    Text Domain: r34npe
    License:     GPL2
    */
    
    /*  Copyright 2016 Room 34 Creative Services, LLC (email: info@room34.com)
    
    	This program is free software; you can redistribute it and/or modify
    	it under the terms of the GNU General Public License, version 2, as
    	published by the Free Software Foundation.
    
    	This program is distributed in the hope that it will be useful,
    	but WITHOUT ANY WARRANTY; without even the implied warranty of
    	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    	GNU General Public License for more details.
    
    	You should have received a copy of the GNU General Public License
    	along with this program; if not, write to the Free Software
    	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    */
    
    // Don't load directly
    if (!defined('ABSPATH')) { exit; }
    
    // Main function for adding noindex tag when appropriate if Yoast SEO *isn't* enabled
    function r34npe_noindex_past_events_without_yoast_seo() {
    	global $post;
    	if (!is_admin() && is_singular() && @$post->post_type == 'tribe_events') {
    		if (!function_exists('tribe_get_end_date')) { return false; }
    		if(!defined('WPSEO_VERSION')){ // Yoast SEO is not enabled. Output noindex tag directly via wp_head.
    			if (strtotime(tribe_get_end_date($post->ID,true,'Y-m-d H:i:s')) < strtotime(current_time( 'Y-m-d H:i:s' ))) { // Event is in the past (using current_time so it uses site's local timezone)
    				echo "\n<!-- noindex Past Events -->\n<meta name=\"robots\" content=\"noindex, follow\" />\n\n";
    			}
    		}
    	}
    }
    add_action('wp_head', 'r34npe_noindex_past_events_without_yoast_seo');
    
    // Main function for adding noindex tag when appropriate if Yoast SEO *is* enabled
    function r34npe_noindex_past_events_with_yoast_seo() {
    	global $post;
    	if (!is_admin() && is_singular() && @$post->post_type == 'tribe_events') {
    		if (!function_exists('tribe_get_end_date')) { return false; }
    		if (defined('WPSEO_VERSION') && strtotime(tribe_get_end_date($post->ID,true,'Y-m-d H:i:s')) < strtotime(current_time( 'Y-m-d H:i:s' )) && get_post_meta($post->ID, '_yoast_wpseo_meta-robots-noindex', true) != 1) { // Yoast SEO is enabled, event is in the past (using current_time so it uses site's local timezone), and Yoast hasn't set the current post to noindex yet.
    			update_post_meta( $post->ID, '_yoast_wpseo_meta-robots-noindex', '1' ); // Set noindex for this event via Yoast SEO instead of outputting own noindex tag.
    
    			// Check if this is a recurring event. We should do the same noindex check/update for the main recurring event.
    			if(function_exists( 'tribe_is_recurring_event' ) && tribe_is_recurring_event()){
    				$recurrence_parent_id = wp_get_post_parent_id( $event_id );
    				if ( empty( $recurrence_parent_id ) ) { // Didn't have a parent so this must be the parent event
    					$recurrence_parent_id = $event_id;
    				}
    				if (strtotime(tribe_get_end_date($recurrence_parent_id,true,'Y-m-d H:i:s')) < strtotime(current_time( 'Y-m-d H:i:s' )) && get_post_meta($recurrence_parent_id, '_yoast_wpseo_meta-robots-noindex', true) != 1){ // Recurring event is in the past and Yoast hasn't set this recurring parent event to noindex yet (we already did the Yoast SEO enabled check.)
    					update_post_meta( $recurrence_parent_id, '_yoast_wpseo_meta-robots-noindex', '1' ); // Set noindex for this parent recurring event via Yoast SEO
    				}
    			}
    		}
    	}
    }
    add_action('template_redirect', 'r34npe_noindex_past_events_with_yoast_seo');
    
    // Add admin page
    function r34npe_admin() {
    	// Missing plugin notice
    	if (!is_plugin_active('the-events-calendar/the-events-calendar.php')) {
    		add_action('admin_notices', 'r34npe_missing_plugin');
    	}
    	// Admin page
    	else {
    		// Placeholder for future admin page
    	}
    }
    add_action('admin_menu', 'r34npe_admin');
    
    // Admin notice for missing plugin
    function r34npe_missing_plugin() {
    	?>
    	<div class="notice notice-error"><p><b>noindex Past Events</b> requires <b>The Events Calendar</b> plugin, but it is missing. Please <a href="plugins.php?s=The+Events+Calendar">activate</a> or <a href="plugin-install.php?s=The+Events+Calendar&tab=search&type=term">install</a> The Events Calendar, or <a href="plugins.php?s=noindex+Past+Events">deactivate</a> noindex Past Events.</p></div>
    	<?php
    }
    

    I would love to see this included officially in the next version of this plugin. Thanks for the great plugin!

  • The topic ‘Proposed Patch: Add Yoast SEO integration’ is closed to new replies.