Categories
WordPress

Excluding a software as a service script from Events Calendar pages and others using Conditional Tags

Recently, I hit a hard page limit with our web governance subscription and decided to exclude lower priority pages such as calendar entries, media attachment pages and blog archive indexes to removing them from the automated check I can avoid upgrading our plan and focus on pages and posts that actually matter.

Last year I created a simple plugin that loads this software as a service into our footer. Here, it’s been extended to omit certain templates using WordPress’ conditional tags method to load it only where it will be useful.

WordPress Templates to be Excluded

  • Archive pages (blog post indexes)
  • Attachment posts (posts generated automatically when a media file is uploaded)
  • All types of Events Calendar pages posts and archives created by the plugin

Templates to be Included

  • Home and front pages of sites
  • Pages
  • Posts
<?php
/*
Plugin Name:  	Monsido Web Governance
Plugin URI:   	https://www.pitzer.edu
Description:  	A plugin to activate Monsido Web Governance on Pitzer's website but only for certain types of pages
Version:      	1.2
Author:       	Joseph Dickson
Author URI:   	https://www.pitzer.edu
License:   	GPL
Date Created: 	January 24, 2018
Date Modified:	March 28, 2019
*/

function pz_monsido_scripts() {

	// Load Monsido script for home and pages, while excluding attachment posts that are linked to pages.
	if ( is_home() || is_page() && ( ! is_attachment() ) ) {

	wp_enqueue_script( 'pz-monsido', plugin_dir_url( __FILE__ ) . 'pz-monsido-web-governance.js', array(), '1.0.0' , false );
	wp_enqueue_script( 'pz-monsido-cdn', '//cdn.monsido.com/tool/javascripts/monsido.js', array(), '1.0.0' , false );

	} elseif ( function_exists( 'tribe_is_month' ) ) {
		// Check if a Modern Tribe Events plugin is running by looking for a function named tribe_is_month. If it doesn't exist move on but most importantly don't fail.

		if ( tribe_is_month() || tribe_is_past() || tribe_is_week() || tribe_is_day() || tribe_is_map() || tribe_is_photo() || tribe_is_event() || tribe_is_venue() || get_post_type() == 'tribe_organizer' && is_single() ) {
	
			// do nothing if it's a calendar post, page or archive

		} elseif ( is_attachment() ) {

			// Check if an attachment page
			// do nothing

		} elseif ( is_single() ) {

			// Check if a single post page and load monsido script.
			wp_enqueue_script( 'pz-monsido', plugin_dir_url( __FILE__ ) . 'pz-monsido-web-governance.js', array(), '1.0.0' , false );
			wp_enqueue_script( 'pz-monsido-cdn', '//cdn.monsido.com/tool/javascripts/monsido.js', array(), '1.0.0' , false );

		} else {
		
			// do nothing

		}

	} elseif ( is_attachment() ) {

		// Check if an attachment page
		// do nothing

	} elseif ( is_single() ) {

			// Modern Tribe not installed on this site  
			// Check if a single post page and load monsido script.
			wp_enqueue_script( 'pz-monsido', plugin_dir_url( __FILE__ ) . 'pz-monsido-web-governance.js', array(), '1.0.0' , false );
			wp_enqueue_script( 'pz-monsido-cdn', '//cdn.monsido.com/tool/javascripts/monsido.js', array(), '1.0.0' , false );

	} else {

		// do nothing

	}

}

add_action( 'wp_footer', 'pz_monsido_scripts' );

You might be wondering, “Why check for the calendar pages?” Well, WordPress will treat an event calendar post like a traditional post unless it’s specifically excluded. earlier. We’ve been using the calendar for a few years now and have at least 1,000 entries that simply don’t need to be reviewed.

By excluding certain types of templates using Conditional Tags I limit the service’s review of the website to primary interest pages and help keep us within the contracted page cap.

Links

By Joseph Dickson

Joseph Dickson is a web developer in higher education and an avid proponent of using WordPress’ core features to create flexible and manageable design solutions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.