Author: topher

Rendering Jetpack Stats

jetpack-logo-smallJetpack is a plugin offered by WordPress.com, and it contains a variety of services, each of which can be activated or deactivated within Jetpack, depending on what you want to use.

One of the features is web site statistics.  You create a WordPress.com account and then associate it with your Jetpack plugin.  Then Jetpack keeps track of statistics for your site on WordPress.com.  You can log in there and view what’s going on, or right in your own site you can view some charts and graphs.

On the sites we build at work we have a Social bar, with the usual colleciton of social traffic indicators.  You can see it here: http://www.salinehornets.com/.  One of the things we wanted there was an indication of how many times the specific articles had been viewed.

Through a long voyage of discovery about why various plugins that claimed to do this were failing, AND some wonderful support from Andy at Automattic, I figured out a good way.

Jetpack provides a function called stats_get_csv that queries WordPress.com, and here’s how I’m using it:

$args = array(
    'days'=>-1,
    'limit'=>-1,
    'post_id'=>$post_id
);

$result = stats_get_csv('postviews', $args);

The -1 indicates infinity, so I’m asking for all days, unlimited, and I’m giving it the post_id I’m looking for.

Then in the function call I’m telling it I want postviews.  That gets me an array that looks like this:

Array
(
    [0] => Array
        (
            [views] => 2
        )

)

So I then do something like this:

$views = $result[0]['views'];
return number_format_i18n($views);

This is all in a function, hence the return.

The stats_get_csv function caches for 300 seconds, so you won’t necessarily see instant changes as you reload your page.  You probably also should be caching on your own end, so that you’re not even hitting it that often.  We’re doing whole page caching, so that my archive page doesn’t make 10 calls at once on that page.

There’s precious little documentation about this process, which is one of the reasons I did this post.

The plugin we tried using before this wasn’t passing the post_id, it was trying to get stats for ALL posts, and then loop through them and grab the one we wanted.  The problem is that the function doesn’t return ALL posts, it returns the top 30 or so.  It’s much more efficient to simply ask for the one you want.

Post Sorting Reloaded

I recently had a client tell me they wanted an archive page that listed every single item in the database on one page.  Since it was for a custon content type and not an infinite blog I went with it. It just uses the default archive template

They they said they wanted them sorted by title alphabetically, rather than by date.  This made me sad because I didn’t want to have to hack together a custom archive page just to change the sorting.  Plugin hunting I went.

Post sorting reloaded screenshot
Admin Screenshot

What I ended up with is Post Sorting Reloaded.  It offers a simple interface to sort ANY content type archive page in any way you want.  Click on the screenshot on the right for a nice large version.

The thing I love most is that it doesn’t try to be clever, and help you figure out how to sort.  It’s simply a wrapper for the posts_orderby filter, and then you can enter any logic code you want for the condition and the sort parameters.

It’s simple, updated recently, and worked magnificently.

WordPress first settings

There’s a short list of things of things I do immediately to every WordPress install.  I’m not talking about plugins or themes, just simple settings changes. Some of them I strongly recommend to everyone, some are optional, but I’d recommend you at least look through them.

 1. Turn off Comments

Even if I know I’m going to want them later, if left on it’ll take about 20 minutes for spammers to find your site and start leaving garbage.  (Settings -> Discussion)

2. Turn off Gravatars

This is optional, but I hate gravatars.  It’s Automattic’s home built spyware and slows your page loads.  (Settings -> Discussion)

3. Set Permalink structure

The default structure is not SEO friendly, nor humanly understandable.  I like “day and name”, but anything beside post id’s is good. (Settings -> Permalinks)

4. Set the Time Zone

The default timezone is UTC, and it’s unlikely that your site is based in that timezone. (Settings -> General)

5. Set the day the week starts on

The default is Monday.  Every client I’ve ever had wanted Sunday.  (Settings -> General)

6. Set Tagline

The default tagline is “Just another WordPress site”.  Occasionally this will remain in the html on the site, but get hidden by CSS or something.  This looks bad to google and other nerds looking at your code. (Settings -> General)

7. Delete the default page and post

WordPress comes with one Page and one Post in there.  It can be ok to re-use the page, but the post needs to go, or you’ll look a little silly when people go to the first post in your blog.  If you don’t rename the first page, or delete it, it also looks bad.

What are some of your initial settings?

Creating a new image size

Recently I found the need to make a new image size in WordPress, other than the usual “Thumbnail”, “Medium”, “Large”, and “Full”.

It’s not very difficult, you simply need to use the function add_image_size in your theme function file, a custom plugin, or an mu-plugins file.

It takes 4 options, 3 are required.  The first is the name of your new size.  This will never be front facing, so use something code-friendly.  Mine was homepage-slide.  Then it takes width and height.  The fourth parameter is whether you want to actually resize the original, or crop it to your new size.

My code looks like this:

if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'homepage-slide', 640, 427, false ); //( NOT cropped)
}

An additional problem that I ran into is that the client had already uploaded about 300 images.  This meant I needed to re-process all of them.

There are several good plugins that do this, but most of them want to do all of your images in one php run, which would time-out for me, since I had so many.

The plugin I used is called AJAX Thumbnail Rebuild. Rather than one big resize call it uses AJAX to make an individual call for each image.  It went through all 300 of my image no problem and then that image size was available to my code.

June Meeting update

Our first meeting was on June 30 at Mutually Human Software and we had about 20 people show up.  We did introductions and talked long about what we wanted from the group and how it would work.

At this point our plan is to meet the third Thursday of every month.  We’ll have one or two main topics as well as have 2-3 people talk briefly about whatever plugin is amazing them at the moment, and wrap up with open conversation.

Kevin Behrens spoke briefly about Role Scoper and Revisionary, describing what each do.

We wrapped up by having open conversation so people could start interacting.

All in all I think it was very successful, and I’m really looking forward to next month’s meeting.

Next Month

Next month’s meeting is also at Mutually Human Software on 21 July.

Brian Richards and Luke Rumley are going to talk about the WPStartbox and Genesis theme engines, and Kevin Behrens will talk some more about Roles.