Forums

[Plugin: MapPress Easy Google Maps] JS files only on pages/posts implementing MapPress (7 posts)

  1. jjprather
    Member
    Posted 2 years ago #

    Great plugin.

    I would like to know if there is a way to have the JS files used by MapPress to only appear in the header of pages/posts that contain a MapPress map.

    Files I am specifically referring to are:
    http://maps.google.com/maps?file=api...
    http://maps.gstatic.com/intl/en_us/mapfiles/159e/maps2.api/main.js

    My situation is this. I have a site that uses MapPress on one individual page. I use YSlow to check load speeds of my site. The second link really is a lag on my load time, and I'd like to be able to only have that weight on my page only when necessary.

    Any ideas?

  2. jstar198
    Member
    Posted 2 years ago #

    This problem bugs me a lot also.. it's not a problem with this plugin in particular but really with the WordPress framework.
    Anyway I had your exact same issue and came up with this hack:
    At the very bottom of mappress.php (in the plugin dir), put a clause similar to the following:

    //MODIFICATION: only load on certain pages
    if (stripos($_SERVER['REQUEST_URI'], 'my-page-name') !== false) {
    	// Create new instance of the plugin
    	$mappress = new mappress();
    }

    Of course change my-page-name to the URL/partial name of your page. You can't use post IDs or other such things at this point since we have not yet entered "the loop".
    This is just a quick & dirty hack but it does work.. ideally someday in the future this sort of thing could be properly built into WordPress.

    Remember that if you update the plugin you will need to re-patch mappress.php

  3. jstar198
    Member
    Posted 2 years ago #

    I forgot this needs to be active on admin pages, here's an updated version:

    //MODIFICATION: only load on certain pages
    $allowed_pages = array('/wp-admin', '/my-page-name');
    foreach ($allowed_pages as $page) {
    	if (stripos($_SERVER['REQUEST_URI'], $page) !== false) {
    		$mappress = new mappress(); // Create new instance of the plugin
    		break;
    	}
    }

    If anyone can think up a nicer implementation please do post it.

  4. jjprather
    Member
    Posted 2 years ago #

    To clarify - that updated code from your last posting goes into mappress.php?

  5. jstar198
    Member
    Posted 2 years ago #

    Yep right at the bottom, instead of the following line:
    $mappress = new mappress();

    Also it might be best to rename the $post variable to something else like $a_page to avoid overwriting the global $post variable.

  6. chrisvrichardson
    Member
    Posted 2 years ago #

    Hi Folks,

    I'll be releasing a new version of MapPress (1.4.5) in the next few days. In the new version the Javascript will automatically load only on pages that actually have a map.

    Please take a look - there are a lot of other great new features as well!

    -Chris

  7. guxte
    Member
    Posted 2 years ago #

    the problem still occurs on version 1.5.8.8
    on my setup. i first tried the function from jstar198, but it seems not to work with urls like "http://domain.com". so it broke my front page for some reason.

    i just made it into an if statement like this:

    if(substr_count(strtolower($_SERVER['REQUEST_URI']), '/maps') || substr_count(strtolower($_SERVER['REQUEST_URI']), '/wp-admin')) $mappress = new mappress();

    not sure if it works on your setup, but it does for me.

Topic Closed

This topic has been closed to new replies.

About this Topic