Skip to content

Instantly share code, notes, and snippets.

@pommiegranit
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pommiegranit/11278396 to your computer and use it in GitHub Desktop.
Save pommiegranit/11278396 to your computer and use it in GitHub Desktop.
A WordPress plugin for building KML files from geocoded posts
<?php
// start of the KML Document
$kml = array('<?xml version="1.0" encoding="UTF-8"?>');
$kml[] = '<kml xmlns="http://www.opengis.net/kml/2.2">';
$kml[] = '<Document>';
$kml[] = '<name>' . $_REQUEST['post_type'] . ' KML export ' . '</name>';
// use query_string as input to WP_Query as this allows good control over the posts being selected - add posts_per_page to get all posts
$args = $_SERVER['QUERY_STRING'] . '&posts_per_page=-1';
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
// add the style information
$kml[] = '<Style id="local-stations">
<BalloonStyle>
<bgColor>ffffffff</bgColor>
<textColor>ff000000</textColor>
<text>
<![CDATA[
<div style="border: 1px solid #a4a4a4; margin-right: 9px; font-size: 0.9em; width: 300px;"><div style="border-bottom:1px solid #999999; margin: 0 0 0 0; padding: 8px 8px 8px 8px; background-color: #e9e9e9; font-weight: bold; font-size: 16pt;"><div style="float: left; margin: 0 0 9px 0;"><img src="http://www.abc.net.au/local/global_img/site_footer/worm_small_footer.png" width="30" height="18" alt="abc logo" /></div>$[name]</div><div style="padding: 8px 8px 8px 8px;"><div style="margin: 0 0 0 5px; float: right;"> <a href="http://www.twitter.com/#!/$[twitterAccount]"><img src="http://www.abc.net.au/local/global_img/generic/icon_social_media_tw.png" width="16" height="16" alt="twitter" border="0" /></a> <a href="$[facebookAccount]"><img src="http://www.abc.net.au/local/global_img/generic/icon_social_media_fb.png" width="16" height="16" alt="facebook" border="0" /></a> </div> <strong style="font-size: larger; font-weight: bold;">Street Address:</strong><br /> $[streetAddress] <br /><br /> <strong style="font-size: larger; font-weight: bold;">Website:</strong><br /> <a href="$[siteURL]">$[siteURL]</a> <br /><br /></div></div>
]]>
</text>
</BalloonStyle>
<IconStyle>
<Icon>
<href>http://www.abc.net.au/local/global_img/maps/icon_worm.png</href>
</Icon>
</IconStyle>
<LabelStyle>
<scale>1.0</scale>
</LabelStyle>
</Style>';
// loop through the posts and create a Placemark for each
while ( $the_query->have_posts() ) {
$the_query->the_post();
$kml[] = '<Placemark>';
$kml[] = '<styleUrl>#local-stations</styleUrl>';
$kml[] = '<name>' . get_the_title() . '</name>';
$kml[] = '<description>' . get_the_excerpt() . '</description>';
$kml[] = '<ExtendedData>';
$kml[] = '<Data name="streetAddress"><value> ' . get_post_meta(get_the_ID(), 'Street-number', true ) . ', ' . get_post_meta(get_the_ID(), 'Street-suburb' , true ) . '</value></Data>';
$kml[] = '<Data name="twitterAccount"><value> ' . get_post_meta(get_the_ID(), 'Twitter', true ) . '</value></Data>';
$kml[] = '<Data name="facebookAccount"><value> ' . get_post_meta(get_the_ID(), 'Facebook', true ) . '</value></Data>';
$kml[] = '<Data name="siteURL"><value> ' . get_post_meta(get_the_ID(), 'Website-URL', true ) . '</value></Data>';
$kml[] = '</ExtendedData>';
$kml[] = '<Point>';
$kml[] = '<coordinates>'. get_post_meta(get_the_ID(), 'Longitude', true) . ',' . get_post_meta(get_the_ID(), 'Latitude', true) . ',0</coordinates>';
$kml[] = '</Point>';
$kml[] = '</Placemark>';
}
}
$kml[] = '</Document>';
$kml[] = '</kml>';
$kmlOutput = join($kml);
header('Content-type: text/xml');
echo $kmlOutput;
wp_reset_postdata();
?>
<?php
/**
* Plugin Name: KML Export
* Plugin URI: http://premium.wpmudev.org/blog
* Description: Exports posts in KML format
* Version: 1.0
* Author: Chris Knowles
* Author URI: http://twitter.com/ChrisKnowles
* License: GPL2
*/
// Template redirection
function get_kml_template( $original_template ) {
if ( isset( $_REQUEST['kml'] ) ) {
$filename = dirname( __FILE__ ) . '/templates/kml-default.php';
if ( isset( $_REQUEST['template'] ) && !empty( $_REQUEST['template'] ) ) {
if ( is_file( dirname( __FILE__ ) . '/templates/kml-' . $_REQUEST['template'] . '.php' ) ) {
$filename = dirname( __FILE__ ) . '/templates/kml-' . $_REQUEST['template'] . '.php';
}
}
return $filename;
} else {
return $original_template;
}
}
add_filter( 'template_include', 'get_kml_template' );
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>station KML export</name>
<Style id="local-stations">
<BalloonStyle>
<bgColor>ffffffff</bgColor>
<textColor>ff000000</textColor>
<text>
<![CDATA[<div style="border: 1px solid #a4a4a4; margin-right: 9px; font-size: 0.9em; width: 300px;"><div style="border-bottom:1px solid #999999; margin: 0 0 0 0; padding: 8px 8px 8px 8px; background-color: #e9e9e9; font-weight: bold; font-size: 16pt;"><div style="float: left; margin: 0 0 9px 0;"><img src="http://www.abc.net.au/local/global_img/site_footer/worm_small_footer.png" width="30" height="18" alt="abc logo" /></div>$[name]</div><div style="padding: 8px 8px 8px 8px;"><div style="margin: 0 0 0 5px; float: right;"> <a href="http://www.twitter.com/#!/$[twitterAccount]"><img src="http://www.abc.net.au/local/global_img/generic/icon_social_media_tw.png" width="16" height="16" alt="twitter" border="0" /></a> <a href="$[facebookAccount]"><img src="http://www.abc.net.au/local/global_img/generic/icon_social_media_fb.png" width="16" height="16" alt="facebook" border="0" /></a> </div> <strong style="font-size: larger; font-weight: bold;">Street Address:</strong><br /> $[streetAddress] <br /><br /> <strong style="font-size: larger; font-weight: bold;">Website:</strong><br /> <a href="$[siteURL]">$[siteURL]</a> <br /><br /></div></div>]]>
</text>
</BalloonStyle>
<IconStyle>
<Icon>
<href>http://www.abc.net.au/local/global_img/maps/icon_worm.png</href>
</Icon>
</IconStyle>
<LabelStyle>
<scale>1.0</scale>
</LabelStyle>
</Style>
<Placemark>
<styleUrl>#local-stations</styleUrl>
<name>ABC Goulburn Murray</name>
<description>Wodonga</description>
<ExtendedData>
<Data name="streetAddress"><value>1 High Street, Wodonga</value></Data>
<Data name="twitterAccount"><value>abcgm</value></Data>
<Data name="facebookAccount"><value>http://www.facebook.com/pages/ABC-Goulburn-Murray/108985902469981</value></Data>
<Data name="siteURL"><value>http://www.abc.net.au/goulburnmurray/</value></Data>
</ExtendedData>
<Point>
<coordinates>146.893163,-36.112082,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment