Roger Stringer   About ▾

I'm Roger Stringer: a father, writer, developer, consultant, chef, speaker. Founder of TheInterviewr.

Code Archives

Grab first image in a post and store it as featured image


In cases of older blogs, authors may not have used featured images, but lately, featured images are used more and more as you can use them to handle your blog layout better.

So what do you do on those posts that have no featured image?

This code is a set of functions that I’ve found handy to add to your functions.php.

It will search a post for the first attached image and store it as a featured image. This code also includes functions to check youtube or vimeo if the post has video instead of images:

In your loop.php, or single.php, or anywhere else you use featured images, you would then call it as such:

Filed Under: Code


Detect high-DPI screens in Google Analytics


Marco Arment shared a trick to capture high-DPI screens in Google Analytics over the weekend, but the code he shared is for an older version of Google Analytics, and I wanted to try this out using the newer version of Analytics, so here’s a copy of the code I use here on this site:

Filed Under: Code


Composing Jolt


When I originally wrote Jolt a year ago, it was meant to stay as one include file, but as it’s grown, I saw the usefulness for doing something more with it.

Jolt is getting used in more and more projects, not just for my own projects, but by more and more developers as well.

After chatting with various Jolt users, some new direction was decided, and today, I pushed Jolt 2.0, which is PSR-0, and also available to install via Composer.

To install Jolt using Composer, you follow these steps:

First, install composer in your project:

    curl -s | php

Next, create a composer.json file in your project root:

    "require": {
        "jolt/jolt": "dev-master"

Now, install via composer:

php composer.phar install

Finally, Add this line to your application’s index.php file:

require 'vendor/autoload.php';

If you don’t want to use Composer, then you can download the Jolt framework into a folder called Jolt and then add the following to the top of your index.php file:

require 'Jolt/Jolt.php';

This changes how some things work with Jolt, and I’ll be updating the various tutorials to use the new version.

I’ve also set up a Legacy branch in the repo to contain the older version of Jolt for anyone who wants to continue using that.

Making this move made sense, it helps to allow for proper updating via Composer, and keeps everything more organized in the long run.

Filed Under: Code


Related Posts without a plugin


This site uses related posts, but it doesn’t use a plugin to load them, instead it uses a weighted search system which is similar to what other related posts plugins use.

I’ve had this code around for a while, and have used it on various other projects, but this is the first time I’ve added it to this site.

This will search based on three criteria: post title, post content, and taxonomies the post belongs to. Each of these are weighted and can be adjusted. I currently place a heavier weight on titles and taxonomies than post content, but that is easily adjustable.

Here’s the class:

You add that to your theme, and include it, then inside your single.php loop, you add this code:

I included the related.class.php file in that code, but you can (and should) include it inside functions.php instead.

What this does, is return a list of related post ids, based on the post title, post content, categories and tags the post is in.

When you pass the call to related, you can customize what appears by setting the following variables: post id, limit, strict limit, and weight which is an array with the keys of body,title and tax.

As seen in this example:

Adjusting the weights will change your related results.

There’s no doubt room for improvement, and as I tweak the code, I’ll update the gist.

Filed Under: Code


Live Blogging with Twilio and Simperium


Live blogging is pretty handy, when attending events, it never hurts to be able live blog about the latest new gadgets.

We’re going to build a simple live blogging app using PHP, Simperium and Twilio.

This particular live blog solution is set up for one event, this means all posts to the phone number we specify will show up.

You could build on this to allow for multiple events, but that’s a little more than we want to dive into today.

And yes, we could also use Pusher for this, but I like Simperium as it serves as both a datastore and a real-time push service, so that way we can store our posts on Simperium, and viewers are updated instantly.

Since these posts are just a line or two or text and a picture.. We don’t need anything too advanced for our data storage…

Since it’s the framework I use for a lot of projects, I once again, built this using the Jolt Micro Framework, which works well for building apps rapidly.

You’ll need a few things things to set this up:

  1. A Twilio account ( )
  2. A account  ( )
  3. You need to create an app ( )
  4. From the app page you need to record:
    1. the “app-id”
    2. the “default” API key
    3. Go to “browse data” and click “generate token” beside your username and generate a token to use for connecting to Simperium.
  5. The Twilio PHP library ( )
  6. The Simperium PHP library ( )
  7. The Jolt framework, as we are going to build this with Jolt. ( )

Let’s get started.

  1. Download the Jolt PHP Framework from here:
  2. Unzip it, open up config.ini, and add the following settings:
  3. Create a folder called Services/
  4. Download the Twilio PHP library here: and save it to the Services/ folder.
  5. Download the Simperium PHP library here: and save it to the Services/ folder.
  6. Open index.php, and copy the following code:
    Our main focus is the /listener section of the code, which is what talks to Twilio.

    When we receive a new message, we check to see if the message had any images or not, and if it did, then we  cycle through each image. We then save the image locally, resize it, and post it to Simperium using the Simperium PHP library. If no image was passed, then we just save the message as it was sent to the system.

    The other part of this is how we get a unique id for each post, which is using the generate_uuid() to create a unique UUID compatible string.

  7. Create a file called functions.php, and copy the following code:
    The main part of this file is the cropResize function which will take any given image and crop and resize it to fit the dimensions we give it. This is handy when dealing with larger sized images.
  8. Open views/home.php and copy the following code:
    This is the brains of our real-time notifications, as we send posts and images to our live blogging app, this is where it will get displayed as soon as the posts are processed.
  9. Upload the files to your web server.
  10. Create a folder called images/ and make it writable, inside this folder, create a folder called original/ and a folder called procesed/, make sure these are writable as well.
  11. Log into your twilio account, and point a phone number to http://MYSITEURL/listener for SMS messages
  12. Open up your web site: http://MYSITEURL/
  13. Send some text or a picture to the phone number and watch it appear on the web site.

We’ve now built a basic live blogging tool, nothing too fancy, but you can take it from here and build on it to make something more fancy.

I’ve also uploaded the demo app to Github, so you can view it in its entirety here:

Filed Under: Code


Easy, nice-looking footnotes with wp-bigfoot


Marco Arment posted about a jquery plugin called “Bigfoot” a few days ago.1

Footnotes are something I’ve been wanting to add to this site, and the style used by Instapaper was one I’ve liked, but hadn’t gotten around to building myself. Bigfoot works nicely by adding that style of footnotes to the site so I quickly turned it into a WordPress plugin.

When installed, you can add footnotes to a post via shortcodes [ footnote] and then end with a closing [ /footnote] so you would have:

Lorem ipsum. [ footnote]My note.[ /footnote]

But without the spaces between [ and footnote.

I'll add some further options later, but at the moment, you get your footnotes showing up as a handy popover where the footnote was specified and also as a list at the bottom of the post.

I’ve also added the plugin to the plugin directory here

Filed Under: Code