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.

By Morten Rand-Hendriksen

Morten Rand-Hendriksen is a staff author at LinkedIn Learning and lynda.com specializing in WordPress and web design and development and an instructor at Emily Carr University of Art and Design. He is a popular speaker and educator on all things design, web standards and open source. As the owner and Web Head at Pink & Yellow Media, a boutique style digital media company in Burnaby, BC, Canada, he has created WordPress-based web solutions for multi-national companies, political parties, banks, and small businesses and bloggers alike. He also contributes to the local WordPress community by organizing Meetups and WordCamps.

7 replies on “How to remove WP Geo plugin from specific pages”

This is definitely good to know for the future,,, I haven’t had to disable specific plugins like this in any of my projects to date… but I figure that this has to have applications in responsive design.

Really good information, keep up the good work. I found your site whilst looking for a solution to a similar problem. I have a website that uses prettyPhoto, lightbox, fancybox and thickbox. I’m currently trying to install a 3rd party plugin, but it conflicts with the above. They are all hardcoded into the theme and child theme, so I am looking for a coder to help me resolve how I can disable lightbox or prettyPhoto on specific pages and posts. From your post here it looks like you have the skills that could solve my problem. Please feel free to email me back if your interested?

At the moment the plugin is geared around being able to set one location per post. Multiple markers have been requested by a few other people but it probably won’t make it into the plugin for a while because there are a few other more popular features to come first. I think I have seen a few other plugins on the WordPress Plugin Repository that may be able to create maps with multiple markers.

Hi,

you solved the biggest problem that I was facing in my Blog. Also, please suggest me some plugin, by which I can share my blog post directly to my fan page. I really need it.

Regards

Prep
Station

well thanks for saving my life today with this function. I have been trying to deregister a script that really only was needed on one page and now I’m all fixed. Thanks sooooo much.

Hello there, You’ve done an incredible job. I will certainly digg it and personally suggest to my friends.
I’m sure they’ll be benefited from this site.

Comments are closed.