Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can control with extreme precision the page of the admin panel where a script must be loaded. The first think you have to consider is that: if the DOM element is not found, the maps API generate the error "a is undefined". So you must load the API in the right page of the admin panel.</p> <p>I use the following script to load the GMaps API in a specific custom post type edit screen:</p> <pre><code>add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts' ); function my_admin_enqueue_scripts( $hook ) { if( ('my_custom_post_type' != get_post_type()) || ('edit.php' == $hook) ) return; // enqueue GMaps API wp_enqueue_script('google-maps-api', 'http://maps.googleapis.com/maps/api/js?sensor=false' ); } </code></pre> <ol> <li>The action hook admin_enqueue_scripts fires just when WP loads the scripts of the admin panel.</li> <li>The argument $hook identifies the current file.</li> <li>my_custom_post_type is ... the custom post type that needs the google map. Of course it could be 'post' or 'page' as well. So, the code above doesn't allow the API to be loaded in pages and posts admin panels.</li> <li>$hook identifies the file edit.php, that is the file that generates the list of posts (or pages, or custom post types). So, if the current page is a list and not a 'new post' or 'edit post' panel, the script is interrupted.</li> <li>The last chance is a 'new' or 'edit' panel of the post type you specified above. Only there the maps API will be loaded</li> <li>Don't forget the custom meta box that will hold the map's DOM element</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload