Created
June 16, 2022 16:03
-
-
Save carmoreira/5b889ef690b68f352dcce2ffcf086b3a to your computer and use it in GitHub Desktop.
Interactive Geo Maps - Custom Solution to read from custom meta fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Interactive Geo Maps Custom Solution | |
* | |
* @wordpress-plugin | |
* Plugin Name: Interactive Geo Maps Custom Solution | |
* Plugin URI: https://interactivegeomaps.com | |
* Description: Reads from custom meta fields | |
* 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_custom_addon', 1 ); | |
/** | |
* Filter map output | |
* @param array $meta - meta data from the map | |
* @return array modified meta | |
*/ | |
function igm_custom_addon( $meta ){ | |
// if we're in the admin, don't do anything, otherwise it will affect map preview | |
if( is_admin() ){ | |
return $meta; | |
} | |
// get the current post/page | |
global $post; | |
$id = $post->ID; | |
// setup the empty arrays that will store our data | |
$regions = []; | |
$legendEntries = []; | |
// get meta fields | |
$map = get_post_meta( $id, 'map', true ); | |
$countries = get_post_meta( $id, 'countries', true ); | |
$colours = get_post_meta( $id, 'colours', true ); | |
// explode the countries and colours to get the arrays | |
$countries = explode( ',', $countries ); | |
$colours = explode( ',', $colours ); | |
// merge both, using $countries as key | |
// this could be done in different ways, | |
// depending on how many parameters you'll need, but since it was just 2 (country + legend ), this works | |
$collection = array_combine( $countries, $colours ); | |
// loop entries. | |
foreach ( $collection as $region => $colour ) { | |
// prepare region entry | |
$entry = array( | |
'id' => trim( $region ), | |
'title' => $region, | |
'tooltipContent' => $region, | |
'content' => $region, | |
'useDefaults' => '0', | |
'fill' => $colour, | |
'hover' => $colour, | |
); | |
// add to regions array | |
array_push( $regions, $entry ); | |
// prepare legend entry | |
$legend = array( | |
'name' => $region, | |
'fill' => $colour, | |
); | |
array_push( $legendEntries, $legend ); | |
} | |
// set map to display | |
$meta['map'] = $map; | |
// set the regions | |
$meta['regions'] = $regions; | |
// create the legend | |
$meta['customLegend']['data'] = $legendEntries; | |
return $meta; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment