Categories
Tutorials WordPress

How to remove WP Geo plugin from specific pages

I ran a cross a rather interesting situation this week while working on the Vi Er Der Du Er site. The site uses the WP Geo plugin extensively on both pages, and posts, and custom post types but I needed to deactivate it for one particular page because I was embedding a different custom Google Map. Not surprisingly the scripts calling my custom map were conflicting with the scripts calling WP Geo and as a result the map on the page in question didn’t work.

At first I thought it was a matter of removing the actions that called the plugin itself. I’ve done this in the past and it works for some plugins. I also found a code snippet here that seemed to show it working. That unfortunately was not the case. So I had to keep digging. Then I found this excellent article How to disable scripts and styles by Justin Tadlock that explained it all: I needed to de-register the scripts, not simply remove the function.

After digging through the plugin code I found the script calls and ended up with this code snippet in my functions.php file:


<?php 

// remove WP Geo JS/CSS from the nybank page
add_action( 'wp_print_scripts', 'vierderduer_deregister_javascript', 100 );

function vierderduer_deregister_javascript() {

	if (is_page_template('page-nybank.php')) {

		wp_deregister_script( 'googe_jsapi' );
		wp_deregister_script( 'jquery' );
		wp_deregister_script( 'googlemaps' );
		wp_deregister_script( 'wpgeo' );
		wp_deregister_script( 'wpgeotooltip' );
		wp_deregister_script( 'wpgeo-admin-post' );

	}
}

?>

As you can see I made the function conditional so it only kicks in when a specific page template is used. You can swap out that condition for any other condition for the same result. Bottom line is it works and now WP Geo works on every page, post and post type except pages that use the page-nybank.php template file.