Title: [Plugin: Basic Google Maps Placemarks] Can I use the shortcode on any php without assign it in funct
Last modified: August 20, 2016

---

# [Plugin: Basic Google Maps Placemarks] Can I use the shortcode on any php without assign it in funct

 *  Resolved [yzqiang](https://wordpress.org/support/users/yzqiang/)
 * (@yzqiang)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/)
 * Hi, I am a old user of your plugin and the translation provider of simplified
   Chinese. I have used the plugin very well and it gives great help to me and other
   friends.
    I am using the plugin in a special circumstance. I want to show one
   placemark on an URL built by 404.php. I use 404.php because I want to catch the
   error by building some non-existing but meaningful URL. For example, the URL 
   includes a place like this: [http://www.mywebsite.com/placea/map.htm](http://www.mywebsite.com/placea/map.htm)
   In 404.php, I want to generate a map when there is map.htm in the URL. But there
   is no page or post, I can’t specify the page or post name in function.php. I 
   hope you can modify the source code to make the map built by shortcode anywhere
   without limitation. Is it possible? If it’s not possible, do you think it’s a
   good idea to use Google Map API directly to build such page? Thanks a lot.
 * [http://wordpress.org/extend/plugins/basic-google-maps-placemarks/](http://wordpress.org/extend/plugins/basic-google-maps-placemarks/)

Viewing 14 replies - 1 through 14 (of 14 total)

 *  Plugin Author [Ian Dunn](https://wordpress.org/support/users/iandunn/)
 * (@iandunn)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913202)
 * Would something like this work inside 404.php?
 *     ```
       if( strpos( $_SERVER[ 'SCRIPT_URL' ], 'map.htm' ) !== false )
       {
         do_shortcode( '[bgmp-map]' );
       }
       ```
   
 * You may have to modify that a bit, but that’s the basic idea.
 *  Thread Starter [yzqiang](https://wordpress.org/support/users/yzqiang/)
 * (@yzqiang)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913241)
 * Hi, I do the code as follows:
    1.In 404.php, I write: <?php do_shortcode( ‘[bgmp-
   map]’ );?> 2.In function.php, I write: function my_theme_name_bgmp_shortcode_check(){
   add_filter( ‘bgmp_map-shortcode-called’, ‘__return_true’ ); } add_action( ‘wp’,‘
   my_theme_name_bgmp_shortcode_check’ ); I trigger the 404 relocation, there’s 
   nothing happened. I check your plugin source code. In core.php line 399, I find
   such code: if( !$post ) return false; Because 404.php is not a post type, the
   source code stop and return here. I delete the code, but I find there are so 
   many judgements of post, I decide not to modify your source code and stop the
   deletion. Can you tell me please why you judge the map must be shown on a post
   type, not just on a php code. If it’s not important, can you remove all the judgements
   in your plugin? If you are busy, I think I can help you to do the job.
 *  Plugin Author [Ian Dunn](https://wordpress.org/support/users/iandunn/)
 * (@iandunn)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913247)
 * The `if( !$post ) return false;` check runs in `mapShortcodeCalled()` and `getMapShortcodeArguments()`
   because they both interact with the global `$post` object.
 * `mapShortcodeCalled()` checks to see if the current post uses the shortcode, 
   and if it does, then the JavaScript and CSS files are enqueued. You can move 
   the code to follow the `bgmp_map-shortcode-called` filter, like this:
 *     ```
       protected function mapShortcodeCalled()
       {
       	global $post;
   
       	$this->mapShortcodeCalled = apply_filters( self::PREFIX .'mapShortcodeCalled', $this->mapShortcodeCalled );		// @todo - deprecated b/c not consistent w/ shortcode naming scheme. need a way to notify people
       	$this->mapShortcodeCalled = apply_filters( self::PREFIX .'map-shortcode-called', $this->mapShortcodeCalled );
   
       	if( $this->mapShortcodeCalled )
       		return true;
   
       	if( !$post )
       		return false;
       ```
   
 * That way you can use the filter to set it from outside the Loop and not have 
   problems with it returning too early because `$post` doesn’t exist. I’ve updated
   that in the code for the next release.
 * Since you’re calling the shortcode from a template, the check in `getMapShortcodeArguments()`
   shouldn’t affect you.
 *  Thread Starter [yzqiang](https://wordpress.org/support/users/yzqiang/)
 * (@yzqiang)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913257)
 * I have tried your way. But I found the code in the same function:
 * > protected function mapShortcodeCalled()
 * There are such codes:
 *     ```
       setup_postdata( $post );
       $shortcodes = $this->getShortcodes( get_the_content() );
       wp_reset_postdata();
       ```
   
 * Because get_the_content function works only in the post, and return nothing in
   404.php. The map can’t be generated until now.
    Thanks.
 *  Thread Starter [yzqiang](https://wordpress.org/support/users/yzqiang/)
 * (@yzqiang)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913262)
 * Can you give me the latest version you updated? Is the version number 1.8.1a?
   
   My email address is yzqiang(at)gmail.com. Tnanks.
 *  Plugin Author [Ian Dunn](https://wordpress.org/support/users/iandunn/)
 * (@iandunn)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913273)
 * If you use the `bgmp_map-shortcode-called` filter to set $this->mapShortcodeCalled
   to true (see the FAQ for details), then mapShortcodeCalled() will return before
   it reaches `setup_postdata( $post )` etc.
 * I’ve updated the SVN trunk if you’d like to grab that.
 *  Plugin Author [Ian Dunn](https://wordpress.org/support/users/iandunn/)
 * (@iandunn)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913274)
 *  Plugin Author [Ian Dunn](https://wordpress.org/support/users/iandunn/)
 * (@iandunn)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913275)
 *  Thread Starter [yzqiang](https://wordpress.org/support/users/yzqiang/)
 * (@yzqiang)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913278)
 * I think there’s some misunderstanding between us. 404.php is not a template. 
   It’s triggered when no URL can be located. It makes get_the_content function 
   get nothing.
    I have updated to 1.8.1alpha, but there’s nothing changed I think.
   Is it possible to get the map out directly by the code down without any preparation
   code? `<?php do_shortcode( '[bgmp-map]' );?>`
 *  Thread Starter [yzqiang](https://wordpress.org/support/users/yzqiang/)
 * (@yzqiang)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913279)
 * My website URL using the plugin is:[http://www.xiaohafo.com/beijing-youaigu/map.html](http://www.xiaohafo.com/beijing-youaigu/map.html).
   
   Please review it to find how it works please.
 *  Thread Starter [yzqiang](https://wordpress.org/support/users/yzqiang/)
 * (@yzqiang)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913281)
 * Because 404.php is not a post, and not connected with a post like template file,
   the plugin doesn’t work.
    I compare the template do_shortcode model with the 
   post shortcode model carefully. I find in do_shortcode model, the source code
   down can’t get the shortcode neither. `$this->getShortcodes( get_the_content());`
   Can you tell me how the plugin can get the shortcode write in the do_shortcode
   argument?
 *  Thread Starter [yzqiang](https://wordpress.org/support/users/yzqiang/)
 * (@yzqiang)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913283)
 *  Thread Starter [yzqiang](https://wordpress.org/support/users/yzqiang/)
 * (@yzqiang)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913286)
 * The job has finished. I did 2 jobs.
    1.Modify the plugin source code of core.
   php in 2 positions. First, comment the function getMapShortcodeArguments.
 *     ```
       public function getMapShortcodeArguments()
       {
       	// @todo - this could be made unit testable if post_content was passed in as param, but would need some kind of controller to wrap it. could then return instead of setting $this->mapshortcodeargs directly
   
       	global $post;
   
       	//if( !$post )
       	//	return;
       ```
   
 * Second, move the judtement postion in mapShortcodeCalled function.
 *     ```
       protected function mapShortcodeCalled()
       {
       	global $post;
   
       	$this->mapShortcodeCalled = apply_filters( self::PREFIX .'mapShortcodeCalled', $this->mapShortcodeCalled );		// @todo - deprecated b/c not consistent w/ shortcode naming scheme. need a way to notify people
       	$this->mapShortcodeCalled = apply_filters( self::PREFIX .'map-shortcode-called', $this->mapShortcodeCalled );
   
       	if( $this->mapShortcodeCalled )
       		return true;
   
       	if( !$post )
       		return false;
       ```
   
 * 2.Add necessary functions in function.php of my theme.
 *     ```
       // 地图插件相关
       // 打开'bgmp_map-shortcode-called'，让页面加载地图所需js
       function xiaohafo_bgmp_shortcode_check() {
       	add_filter( 'bgmp_map-shortcode-called', '__return_true' );
       }
       add_action( 'wp', 'xiaohafo_bgmp_shortcode_check' );
   
       // 设置地图参数
       function setBGMPMapShortcodeArguments( $options ) {
       	if (strpos($_SERVER["REQUEST_URI"], 'map.html') !== false) {
       		$arr_uri = explode('/', $_SERVER["REQUEST_URI"]);
        		if (is_array($arr_uri)) {
        			$bgmp = lhk_get_post_by_slug($arr_uri[1]);
        			if (is_object($bgmp)) {
       			    $options[ 'mapWidth' ]		= 725;
       			    $options[ 'mapHeight' ]		= 600;
       			    $options[ 'latitude' ]		= get_post_meta($bgmp->ID, 'bgmp_latitude', true);
       			    $options[ 'longitude' ]		= get_post_meta($bgmp->ID, 'bgmp_longitude', true);
       			    $options[ 'zoom' ]			= 12;
       			    $options[ 'type' ]			= 'ROADMAP';
        			}
        		}
       	}
           return $options;
       }
       add_filter( 'bgmp_map-shortcode-arguments', 'setBGMPMapShortcodeArguments' );
       ```
   
 * 3. Show the map in the 404.php.
    `echo do_shortcode('[bgmp-map"]'); ?>` At last,
   the map shows on my URL by 404.php, not by post or template file. Thanks for 
   all the infomations you provided.
 *  Plugin Author [Ian Dunn](https://wordpress.org/support/users/iandunn/)
 * (@iandunn)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913307)
 * Cool, thanks for posting the solution 🙂

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘[Plugin: Basic Google Maps Placemarks] Can I use the shortcode on any
php without assign it in funct’ is closed to new replies.

 * ![](https://ps.w.org/basic-google-maps-placemarks/assets/icon-128x128.png?rev
   =1152531)
 * [Basic Google Maps Placemarks](https://wordpress.org/plugins/basic-google-maps-placemarks/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/basic-google-maps-placemarks/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/basic-google-maps-placemarks/)
 * [Active Topics](https://wordpress.org/support/plugin/basic-google-maps-placemarks/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/basic-google-maps-placemarks/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/basic-google-maps-placemarks/reviews/)

 * 14 replies
 * 2 participants
 * Last reply from: [Ian Dunn](https://wordpress.org/support/users/iandunn/)
 * Last activity: [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-basic-google-maps-placemarks-can-i-use-the-shortcode-on-any-php-without-assign-it-in-functionphp/#post-2913307)
 * Status: resolved