Skip to content

Instantly share code, notes, and snippets.

@toolness
Created August 14, 2014 19:34
Show Gist options
  • Save toolness/b5416a7eb2d27dd4d263 to your computer and use it in GitHub Desktop.
Save toolness/b5416a7eb2d27dd4d263 to your computer and use it in GitHub Desktop.
Wordpress plugin to list Hive member organizations from a Hive Directory server.
<?php
/**
* @package Hive_Directory_Listing
* @version 1.0
*/
/*
Plugin Name: Hive Directory Listing
Plugin URI: http://github.com/toolness/hive-django
Description: This is a plugin that integrates your WordPress site with the Hive\
directory.
Author: Atul Varma
Version: 1.0
Author URI: http://toolness.com/
*/
function hive_directory_listing_func($atts) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $atts['url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$members = json_decode(curl_exec($ch), TRUE);
curl_close($ch);
$html = '';
for ($i = 0; $i < count($members); $i++) {
$m = $members[$i];
$html = $html . '<a href="' . $m['website'] . '">' .
$m['name'] . '</a><br>';
}
return $html;
}
add_shortcode( 'hive_directory_listing', 'hive_directory_listing_func' );
?>
@toolness
Copy link
Author

Currently this just registers a shortcode that can be used like so in a Wordpress page/post:

[hive_directory_listing url="http://localhost:8000/api/v1/cities/nyc/members"]

This will just insert a list of links into the page/post similar to the one on the current members page of the Hive NYC site.

This is just a super early initial version. Things left to do:

  • Instead of providing a full URL, I'd rather have it use directory.hivelearningnetworks.org as the default base URL, and simply take a city slug instead of requiring the wordpress user to know the full URL structure of the REST API.
  • We might as well sanitize the JSON we're receiving via htmlspecialchars (for text) and filter_var (for urls), just to be extra safe.
  • There's an extra <br> at the end of the output that should be removed.
  • Might want to ask around the Wordpress community and see if there's a better way to do this kind of thing. Specifically, it'd be nice if Wordpress users could have control over the HTML template used to show the list, so they could e.g. put it in an actual <ul> if they wanted, or something else. I originally wanted to use the Shortcode Exec PHP plugin instead of rolling my own plugin, but it doesn't seem to be maintained anymore.

Note that this plugin is meant to integrate, eventually, with toolness/hive-django#34.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment