WordPress.org

Ready to get started?Download WordPress

Forums

Pronamic Google Maps
[resolved] Load script only on pages (post types) with maps? (13 posts)

  1. MrVictor
    Member
    Posted 3 years ago #

    Hi,

    The Google Maps scripts load on every page right now! When I select one of my custom post types it should really only load when viewing my custom post type's pages.

    http://wordpress.org/extend/plugins/pronamic-google-maps/

  2. Thomas Jaggi
    Member
    Posted 3 years ago #

    +1

  3. Remco Tolsma
    Member
    Plugin Author

    Posted 3 years ago #

    Thanks for your advice, we adjusted some things in our plugin:
    http://plugins.trac.wordpress.org/changeset/388776/pronamic-google-maps

    On archive pages, and other special pages, the script will still always load. It's not easy to tell if the script is not needed on these pages. Do you have an idea how to optimze this further?

    if(is_singular()) {
    	if(pronamic_google_maps_is_active()) {
    		wp_enqueue_script('pronamic-google-maps-site');
    	}
    } else {
    	wp_enqueue_script('pronamic-google-maps-site');
    }
  4. MrVictor
    Member
    Posted 3 years ago #

    Okey.

    Maybe you could give admins the opportunity to choose wich pages the script should load on? For me it would be enough to show it on single pages of a custom post type and on one page.

  5. Thomas Jaggi
    Member
    Posted 3 years ago #

    Couldn't it be part of the map rendering?

  6. Remco Tolsma
    Member
    Plugin Author

    Posted 3 years ago #

    I don't think that is possible, scripts have to be enqueued before wp_head() is executed.

    http://codex.wordpress.org/Function_Reference/wp_register_script

  7. Thomas Jaggi
    Member
    Posted 3 years ago #

    Scribu presents some nice solutions: http://scribu.net/wordpress/optimal-script-loading.html

  8. Remco Tolsma
    Member
    Plugin Author

    Posted 3 years ago #

    Thanks for the link, I made some adjustments and this could work. However the Google Maps API documentation says that the scripts should be place in the <head> section.

    http://code.google.com/intl/nl/apis/maps/documentation/javascript/tutorial.html#Loading_the_Maps_API

    With the solution from Scribu the scripts are placed in the footer (before the </body> tag). Do you have any idea what kind of effect this will have?

    The solution is working in Firefox 3.6.16, Internet Explorer 9 and Google Chrome...

    Some more related links:
    - http://beerpla.net/2010/01/15/follow-up-to-loading-css-and-js-conditionally/
    - http://beerpla.net/2010/01/13/wordpress-plugin-development-how-to-include-css-and-javascript-conditionally-and-only-when-needed-by-the-posts/

  9. Thomas Jaggi
    Member
    Posted 3 years ago #

    I guess Google just assumes that a map should load as early as possible. But as far as I know it works just fine if you include the scripts at the end of your markup.

  10. Remco Tolsma
    Member
    Plugin Author

    Posted 3 years ago #

    Ok, thanks. We used the solution from Scribu in version 1.8:
    http://wordpress.org/extend/plugins/pronamic-google-maps/

  11. Thomas Jaggi
    Member
    Posted 3 years ago #

    Great!

  12. pintofmilk
    Member
    Posted 2 years ago #

    I am running wp_enqueue_script('pronamic-google-maps-site'); in functions-user.php when the page loads but the script is not being inserted into the HTML code.

    Other scripts are being properly enqueued from the same code block - do I need to add something else?

  13. Remco Tolsma
    Member
    Plugin Author

    Posted 2 years ago #

    Do you enqueue the script within the 'wp_enqueue_scripts' routine?

    function prefix_enqueue_scripts() {
    	wp_enqueue_script('pronamic-google-maps-site');
    }
    
    add_action('wp_enqueue_scripts', 'prefix_enqueue_scripts');

    http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic