All blog entries reflect the opinions of the author and have not been expressly endorsed by the Ivan Allen College of Liberal Arts or the Georgia Institute of Technology.
One of my recent adventures has been trying to build a WordPress plugin to display faculty profiles pulled from our college's Faculty / Staff Directory Service. This has been quite a wild ride, as I came into this knowing nothing about WordPress plugin development, albeit I'm a very experienced PHP coder and have been writing Drupal modules for several years now. It also turns out that there's no straightforward way of porting my faculty/staff profile Drupal module over to WordPress, so below I'm going to attempt to document the hoops I jumped through to make this work. If you are on-campus or on the VPN, you can see an example of the plugin in action here:
http://pwp-dev.gatech.edu/webdev/bio/
In Drupal, it's incredibly easy to implement a module that generates a dynamic page at a custom URL path. You simply create your own hook_menu() function to define your URL path in Drupal's core 'menu' tree, pointing that path to a content generation function in your module. That function does all the dirty work, returning your dynamic page content as a string, which then gets rendered on the screen inside of your theme template's page framework.
It's not so simple in WordPress. The url path system isn't easily altered via a plugin, and WordPress wants everything that shows up on the screen to be in the form of a 'post' coming from its database. So, that leaves you with just a few basic options:
- Figure out how to create your own custom post type and then implement a custom display and editing interface for it. The popular Tribe Event Calendar plugin takes this approach, but it's very complicated and I never could figure out exactly what they are doing in that plugin.
- Hook the action 'parse_request', then check
$wp->query_vars['pagename']
to see if it matches your virtual path, and if so then add another hook onto the filter 'the_posts' to generate a fake post. This seems to be a workable solution, but generating a fake post is very messy, and you also have to do a little CSS magic to hide the auto-generated 'Edit' link, which won't work on a fake post. The anatomy of a fake post that I used is based on this solution that I found on StackOverflow: http://stackoverflow.com/questions/17960649/wordpress-plugin-generating-...
I ended up implementing option #2 as a means of being able to dynamically generate a page showing the details on any known publication for the faculty member in question. However, I came up with a more straightforward and flexible way of inserting the main profile onto a page. Instead of trying to generate a profile page automatically, I opted for letting the user choose to create a page with the slug 'bio', and then on that page they can have whatever text they want with the following replacement token embedded within the text, where 'gpburdell3' would be the faculty member's own GT Account Username):
[[gpburdell3]]
When the page is viewed, that token gets replaced with the user's profile as pulled from the college's Faculty/Staff Directory server.
To implement this concept, I add a hook onto the filter 'the_content' so that I can parse the page content and do a search-and-replace on the profile token. For the sake of speed, I only do this on a page with the slug 'bio' by wrapping my search-and-replace code with a simple check of is_page('bio')
.
All other code in the plugin is just lifted straight from my Drupal module. That code handles making the backend request to our central Faculty/Staff Directory Server via a REST based API and formatting the results in proper HTML.
Unfortunately, in some ways this all feels like a big collection of hacks, but at the moment it does seem to work. I should note that I did all of this development against the latest WordPress, 4.2.2, so there's no guarantee that these techniques will work on older versions. Then again, you really shouldn't be running any version before 4.2.2 in production according to the security experts.
Comments on these techniques are welcome -- just use the my Contact page to get in touch with me.