Tinkering With Data Consuming WordPress Plugins

Now that I’ve got my own webspace on Reclaim Hosting and set up a handful of my own self-hosted WordPress blogs, I started having a tinker with some custom plugins. I’ve started off with something simple, a couple of shortcode plugins that I can use to embed a simple leaflet map into a post or a page.

datawire_–_test_blog___Just_seeing_what_the_bots_can_do…

So far, I’ve tried a couple of different approaches…

The first one has been pared down to just a default accepting shortcode:

[IWPlanningLeafletMap]

The shortcode calls a bit of code that makes a cached, (at most, three times a day) request to a morph.io scraper that returns a list of currently open for consultation planning applications to the Isle of Wight Council. (That said, I can pass in a few shortcode parameters relating to the size of the map (which is a fixed size, unfortunately – I haven’t worked out to make it flexible…) and the default location and zoom.) The scraper itself runs once daily.

scraperplugin

My feeling is that this sort of code would work best in the context of a WordPress page, acting as a destination that allows folk to just check on currently open applications.

The second plugin embeds a map that displays markers for recent house sales (as recorded by the Land Registry prices paid dataset). This dataset is published as a monthly set of data a month or two after the fact and is downloaded to my local desktop. A python script then reads in the data, creates a new WordPress post containing the shortcode with the data baked in, and uploads the post to WordPress (where it appears in the draft queue).

 alt=

In this shortcode, the marker data is currently encoded using the PHP serialise format (via the python phpserialize dumps method) and embedded in the post as the value of shortcode attribute.

[MultiMarkerLeafletMap zoom=11 lat=50.675 lon=-1.32 width=800 height=500 markers='a:112:{i:0;a:5:{s:3:"lat";d:50.699382843;s:4:"date";s:10:"2015-07-10";s:3:"lon";d:-1.29297620442;s:8:"location";s:22:"SAVOY COURT, TOWN...]

In this case, with the marker data baked into the shortcode, there’s a good argument for rendering the map within a timestamped post as a fixed map (at least, ‘fixed’ in the sense that the data is unchanging).

The PHP un/serialize route isn’t ideal because I think it raises security issues? I originally tried to pass the data as serialised JSON, but the data is in the form of a list and it seems the ] breaks things. I guess what I should really do is see if I can pass the data in as serialised JSON between the shortcode tags rather than pass it in as an attribute?

Another approach might be to define just a simple map embed shortcode, and then support additional shortcodes that add markers to the map?

My PHP coding is also a bit scrappy (and I keep forgetting the ;’s…:-( I think one thing I do need to do is pop the simple plugin code functions into a class to keep them safe, and also patch in a hack or two (as seems to be required?) so that the leaflet map libraries are only loaded into post and page headers for posts/pages that actually contain one of the map shortcodes.

I’m also thinking now I need to find a way to support boundary lines and shape colouring?

boundarymaplines

Hmm…

Author: Tony Hirst

I'm a Senior Lecturer at The Open University, with an interest in #opendata policy and practice, as well as general web tinkering...

5 thoughts on “Tinkering With Data Consuming WordPress Plugins”

  1. Just at a glance I see that other maps, also image sliders, don’t pass the params in a shortcode, but reference by id another WordPress construct that holds the data like [map databomb=”3″] I’d guess they create custom post types then to hold the actual data and the script extracts it knowing the right ID where it’s stored

    1. @alan Maybe… I’m still trying to find my way… One thing I want to be able to do is get the data in via an xml_rpc push (as per the second example); I’m still not totally clear how to use xml_rpc, eg whether I can use it to load stuff into my own database tables, or how to work with other WordPress elements, whether native, custom, or plugin. For example, TablePress (https://tablepress.org/) looks like it could be interesting for my needs.

      By the by, do you know of a clean/elegant way of setting up a shortcode plugin so that any additional assets (js, css etc) loaded in by adding to the post/page header only get added for posts/pages that include the shortcode?

    2. Alan.. hmm… so thanks for the tip re: custom post types – https://codex.wordpress.org/Post_Types
      So I guess I could define a serialised JSON blob post type, and then just make a custom post over XML-RPC (not sure if it lets you post to a custom post type out of the can or whether I’d also need to define a custom method as per https://codex.wordpress.org/XML-RPC_Extending and for my purposes called using eg http://python-wordpress-xmlrpc.readthedocs.org/en/latest/examples/custom-methods.html )

      Hmm.. seems like I won’t be getting tonight off then…!;-)

Comments are closed.