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.
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.
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.
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.
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.
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.
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]' );?>
My website URL using the plugin is:http://www.xiaohafo.com/beijing-youaigu/map.html.
Please review it to find how it works please.
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?
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.
Cool, thanks for posting the solution 🙂