Skip to content

Instantly share code, notes, and snippets.

@carmoreira
Created May 2, 2021 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carmoreira/1e23c1100828a3ea7ffc2cb7047c118a to your computer and use it in GitHub Desktop.
Save carmoreira/1e23c1100828a3ea7ffc2cb7047c118a to your computer and use it in GitHub Desktop.
Interactive Geo Maps - Custom Addon to populate map from Custom Post Type
<?php
/**
* Interactive Geo Maps Callback
*
* @wordpress-plugin
* Plugin Name: Interactive Geo Maps CPT Addon
* Plugin URI: https://interactivegeomaps.com
* Description: Connects CPT with map
* Version: 1.0.0
* Author: Carlos Moreira
* Author URI: https://cmoreira.net/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
add_filter( 'igm_add_meta', 'igm_cpt_addon', 1 );
/**
* Filter map output
* @param array $meta - meta data from the map
* @return array modified meta
*/
function igm_cpt_addon( $meta ){
// if you want to target a specific map, use this code with your map ID
/*
if ( intval( $meta['id'] ) !== 999 ) {
return $meta;
} */
// you can modify/populate the different types of data. In this example we'll modify 'regions'
// but you could modify 'roundMarkers', 'iconMarkers', 'vectorIcons' and 'labels'.
$post_type = 'tshowcase'; // modify here to set your own custom post type
// we will query the custom post types and loop through them
$args = array(
'posts_per_page' => -1,
'post_status' => 'publish',
'post_type' => $post_type,
);
$posts = get_posts( $args );
$regions = [];
// loop posts.
foreach ( $posts as $post ) {
// in this example we'll get the region code from a custom meta field
// if you're using ACF you can use the function get_field instead
$entry = array(
'id' => get_post_meta( $post->ID, '_tslocation', true ), // this should be the region name or code according to selected map
'title' => $post->post_title,
'tooltipContent' => $post->post_title,
'content' => $post->post_content,
'useDefaults' => '1', // set this to 1, to allow the regions to inherit colors from default settings. If you want to control the colours populate the 'fill' and 'hover' parameters
'customField' => get_post_meta( $post->ID, '_mycustommeta', true ),
//action => 'open_url',
//latitude => 0,
//longitude => 0,
);
// you can populate the entry with whatever custom fields you need. You can then use {customField01} in the tooltip template, to display information from those fields
array_push( $regions, $entry );
}
$meta['regions'] = $regions;
return $meta;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment