Textpattern plugins and Facebook apps
Since Chris and I have become addicted to Facebook, we decided it would be cool to have a plugin for our respective blogging engines (Chris uses WordPress, I use Textpattern) that would update our Facebook accounts’ mini-feeds when we posted something on our blogs. Thus we both set off on our epic missions to create Facebook applications and WordPress / Textpattern plugins that would support this behaviour.
Here I’ll relate my experience with my Textpattern plugin. I’ll leave Chris to talk about his experience with WordPress.
So yeah, basically, my experience was a nightmare.
This was my first attempt ever at writing a Textpattern plugin so I am by no means an expert at this. It took me a while to figure out how to compile my php code so that I could deploy it to my Textpattern instance on my web server. Then it took me another while to figure out how to hook into Textpattern ‘events’. I must say, the documentation for this is really poor and I found myself trawling through the Textpattern source to figure out what functions did what. Not pretty. The best (and only) sites I found that helped me somewhat were this article on threshold state and this Textpattern source repository.
When I finally figured out how to call the ‘register_callback’ functions and that I had to use ‘article’ as the event I wanted to register my own events against, I was able to get stuck into it.
My next challenge was how to add a checkbox to the ‘write’ tab of the admin screens. I wanted a checkbox that when checked, would essentially call my code to notify my facebook account of my post. If it wasn’t checked, it wouldn’t notify facebook. However, adding a checkbox and positioning it where ever I want in the flow of elements on the page was NOT an easy task. Below is a sample of the code I had to use to get this to work:
1 2 3 4 5 6 7 8 9 10 | function mdt_event_insert_checkbox($buffer) { $fb_notify = graf('<small><label for="notify_facebook">'. checkbox("notify_facebook", '1', '1', '', 'notify_facebook'). 'Notify Facebook?</label> '); $find = '<input type="submit"'; $replace = $fb_notify.$find; return str_replace($find, $replace, $buffer); } |
NASTY! I had to find the ‘Save’ button, then replace the HTML with my new HTML that includes the checkbox. OMG.
Anyway, I finally got that working (see screenshot below).
Next I needed to grab the title of the new post, the friendly-url to the article on my site and the excerpt of the new article. This again was no easy task. The following code seems to do the trick:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function current_url() { return 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } function get_latest_post() { include_once txpath.'/publish/taghandlers.php'; // get the latest article posted $rs = safe_row("ID, Title, Excerpt", 'textpattern', "1=1 ORDER BY LastMod DESC LIMIT 0,1"); $title = ' has blogged: ' . urlencode(stripslashes(urldecode('<a href="' . permlinkurl_id($rs['ID']) . '">'))) . substr($rs['Title'], 0, 45) . '</a>'; $msg = substr($rs['Excerpt'], 0, 180); $url = current_url(); if (strtolower($step) == "publish") { $url = $url . "?event=article"; } elseif (strtolower($step) == "edit") { $url = $url . "?event=articlex%x%step=editx%x%ID=" . $rs['ID']; } } |
So now the tricky part – how to connect to Facebook, log in as myself and call the Facebook API function that updates my ‘mini-feed’ with some content. I first attempted this by embedding the Facebook API into the Textpattern plugin so that the integration would be completely seamless. This, however, was difficult on many levels. First of all, I couldn’t get the Facebook API to compile into my Textpattern plugin. The php compiler would tell me that certain server variables weren’t defined, etc etc. I did iron out all those error messages and warnings eventually though and finally got the code to compile. However, I later realized that this was never going to be a feasible solution because in order for this to work, my Facebook developers’ API key and secret key would need to be hard-coded into the Textpattern plugin and if I were ever to make this Textpattern plugin public (which I intend to do), I would be sharing my API and secret keys, something I don’t necessarily want to do. Doh! So I had to change my thinking somewhat.
The solution to this was to basically write the Facebook part of the plugin as a totally separate web application and have the Textpattern plugin call this web app, passing through to it the title, url and excerpt in the form of a query string. This method lets the Facebook web application handle all the Facebook functionality (logging in and authenticating, remembering you once you’re logged in, calling the appropriate API functions, etc etc) and the Textpattern plugin basically doesn’t need to know anything about Facebook.
What a mission.
However, it all seems to be working now and I’ve used it once already. Those of you who are on Facebook and have added me as a friend will be able to see little posts on my mini-feed (on my profile page) saying “Annie has blogged…” with a link through to this article. Very cool :) When I am comfortable that things are working as intended and I tidy things up enough to make my Textpattern plugin publicly available, I’ll put it up on my here somewhere for people to grab.
Phew

September 9th, 2007 at 12:22 am
I would love to get my hands on that plugin as soon as you get it up and running. Thanks for doing this.
January 16th, 2009 at 9:28 am
Finally did you have released the TXP-FB plugin? Let me know, thanks! :)
February 27th, 2009 at 9:41 pm
No, sorry – I never did finish it properly. It was such a hassle, having to hack up the Textpattern code in order to catch the right events and submit to Facebook. As you can probably tell, I’ve moved onto Wordpress now and haven’t really looked back. Sorry!
June 12th, 2009 at 8:26 pm
WOuld you mind to share the partial code you have? I MIGTH be able to finish it. Thanks!