Posted on

A very WordPress centric few weeks!

Not a very interesting blog post but just some thoughts about the kinda things I’ve been up to recently. Specifically WordPress! More specifically: Talking about WordPress!

I did my first talk at MWUG last month. Man was it scary going up there and rambling to 50 or so people. Maybe it wasn’t quite 50, but it felt like a lotta people! Everyone was saying the fear will go away when you get up there and start talking, I didn’t find that, I was actually screaming internally most of the time. That’s just me being dramatic, though. The talk went will, people clapped and I got asked questions I could answer – it was like I knew what I was talking about! I guess that’s the key, just know what you’re talking about.

I am pretty critical and feel I can do better next time, but hurray, I spoke in public! Y’know what, I did it twice!



This is me talking at MWUG and then a week later talking at WPLeeds with Keith. Keith is so chill and sounds ace when he speaks – it balances out my high pitched ‘HI I KNOW HOW TO WORDPRESS’ voice. My first talk was on duplicating your WordPress website to a staging site, for safe development. Keith and I tackled common support issues together, and that’s the talk we’ll be taking to WordCamp Manchester.

The great thing about doing it at Leeds first was that we got some really good feedback. There’s plenty to add and expand on, so I’m feeling pretty confident that we’ll have a good talk down for the main event.

Further to talking about WordPress, I’ve been developing my own plugin. Which is actually going pretty well. I’m a fairly novice coder and typically alter existing scripts that almost do the thing I want so they actually do the thing I want. This was different, I’ve been coding the plugin from start to finish and I’m pretty darned excited to get it out there. It’s basic as heck – it’ll just be a shortcode which puts your current Fitbit steps for the day anywhere you like.

But you’d be surprised how much I’ve had to learn to get that working. I’ve learned about WP_CRON and how to create my own CRONs using curl in PHP. I’ve also nearly perfected my own OAUTH2 script, which Dav said is pretty hard to do! That said, the reason I’ve not pushed to finish the plugin just yet is because I’ve hit a pretty bad encoding issue which I can’t get past. So hopefully one of the fine developers I’ve been bombarding with questions for the past week can help me overcome that. But yes, I feel like I’ve gained some pretty rad knowledge in the past few weeks.

That’s it really, not interesting, but I wanted to blog this little milestone of now being a WordPress talker type person! I’ve been making WordPress websites (for fun) for years, and I totally enjoy bringing that to work. Oh boy, I should blog about some of the terrible website ideas I’ve had in the past. Anyway, laters!



 

Posted on

Some rambling about my Fitbit Plugin

The main aim of this post is for my own reference so I vaguely recall the coding I’ve done over the past few days so I don’t forget it and can continue to develop this thing after a few days break from it.

So, I’ve wanted to code a Fitbit plugin for ages – just a nice simple one which displays my steps as I walk throughout the day.

Now, sadly that has been a million times harder than I ever expected, and after some minor research I decided it was way over my coding skills to create one. Mostly due to a lack of understanding in how I can get JSON data from Apis and convert it into useable PHP.

That kinda changed after I bought my Hue Go lamp – blog post on that coming soon! It had a debugging API which showed me how to use JSON and cURL to change my lights via command line.

Anyway, my brain clicked and I realised I needed to use cURL to grab the data I needed. With this info I was able to find Fitbit’s debug tool, use it to get my access codes for the Fitbit, and I was able to set a cron which updated a JSON file with the latest step count. From there I used a PHP file to decode and display the steps.

That works fine – you can see it working to the right hand side in my sidebars. The issue is, it doesn’t work constantly. The access token changes, and breaks the code. This left me with a to-do list:

  • I needed to change my cron from running via crontab, and instead I implemented it to run via WP_CRON – which is ace, because I learned a lot about plugin development from that. So right now I have a plugin which I activate and it sets the cron.

So my cron runs perfectly fine, but the steps will only display for as long as the hard coded access token will allow.

So, the only real way to make this thing work is to learn how to use Oauth2, and it’s been a headache! Especially for a novice coder like myself. However I’m starting to get there.

Currently my in progress plugin is set up as follows:

  1. Upon activation the cron is added to my event list, the cron initiates a cURL request every ten minutes to check my Fitbit activity. After cURL’ing the data, it dumps the JSON into a file.
  2. From there, I have a PHP file which uses json_decode to parse the data into a PHP array. I am then, very crudely inserting the PHP into a widget. The intention is to instead get a shortcode created so I can add this whoever I want.
  3. I decided to delve into the Oauth2 stuff, and after looking at so many libraries that other people had set up specifically for people to use the Fitbit API – I decided I was just going to code something myself because I wasn’t getting anywhere.
  4. Using the Fitbit decoder, I’ve got myself to a point where I can generate access tokens – There is a ton of useless code in this now as I realised it was as simple as using curl_setopt to post my client ID, secret key and other application settings to the Fitbit OAuth site. This led the curl to respond with a bunch of JSON – including a new and valid access key.

So I can generate access keys from within my plugin’s admin panel now. However I then have to take the code and hard code it into my cron page. So I’m like… Half way towards what I actually need. Here’s my plan of action:

  1. Fix up the plugin admin page so the application information (ID, secret key, redirect URL) are not hardcoded and instead can be inserted into a form which I guess would store them in the database. That all sounds do-able, just time consuming.
  2. Store the access token in the database also – because then I can pull it into my cron, and it can be overwritten when the code is refreshed.
  3. Code refreshing – this sounds hard. I think I need to set up another cron, which runs the CURL to grant a new access token. From there I can decode it and throw the new access token into  my database entry. That said I haven’t looked much into this part.
  4. When that’s all sorted I’mma fix this so that I can use shortcodes to display my steps.

When all this is done I’ll be fairly proud of myself, and maybe I’ll start looking at adding other metrics for it, most of the code I have implemented also imports calories intake, weight and the other junk Fitbit stores, but it doesn’t do anything with it.

Phil and Dav have really helped me get this working, they deserve cakes or something. I shall post a blog of super happiness when I have finished with this little adventure!