get_template_part_content for Single Posts

I’ve been looking for a non-intrusive way to insert text before any of my WordPress Single Posts and Pages. In the past, I modified the themes directly, but since I didn’t own the themes I was using, anytime I updated the theme, I would lose all my changes.

I had found I can insert text at the top and bottom of my pages using add_action. For example:

add_action( 'get_footer', 'echo_hello_world' );

function echo_hello_world( $post )
{
   echo "hello world";
}

would add the phrase “hello world” to the bottom of every single page on my blog. You can find a list of tags on WordPress’ Action Reference page.

One of the things I wanted to do was insert some text beneath my header, but before my post. I had found get_template_part_content would do the trick on Pages, but not Single Posts. After I failed at finding a solution online, I looked into how the code differed between Pages and Single Posts.

Single Posts were calling:
get_template_part( 'content-single', get_post_format() );

while Pages were calling:
get_template_part( 'content', 'page' );

So I wondered if get_template_part_content_single would work, but nothing happened. I then tried get_template_part_content-single, lo and behold, it WORKED! Given that Google has 0 search results with that exact phrase, I’m guessing no one’s discovered this action tag or at least posted about it publicly.

So my code ended up looking like:

add_action( 'get_template_part_content', 'echo_hello_world' );
add_action( 'get_template_part_content-single', 'echo_hello_world' );

function echo_hello_world( $post )
{
   if( is_single() || is_page() )
   {
       echo "hello world";
   }
}

As a bonus, here’s a tag you can use to insert text between your post and the comments section: comments_template

Exclude WordPress Categories from your Main Page or Feed

So for a long time, I’ve gotten tired of having my Twitter logs fill my blog’s main page, and since I’ve winded down the HD-Trailers.net blog (moved everything to the main site), I didn’t want that to fill the blog up with Twitter spam either. But I love the archive ability of having my Twitter messages saved into my blog. It makes it a lot easier to search for stuff I’ve tweeted about.

After a bit of searching, I found this WordPress plugin: Ultimate Category Excluder. It allows you to exclude any category from your main page, feed, and archives.

As of now, I’ve excluded the Twitter category from the main page and feed, so it’ll still show up in the archives.

Pretty neat!

You are too awesome for Ping-o-matic.

If you’re trying to automate pinging Ping-o-Matic! via PHP/cURL, you may have hit into an issue where the response you get is:

You are too awesome for Ping-o-matic.

To fix this, all you have to do is add a user-agent to your curl options, e.g.
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0" );

Once you set a user-agent, you should be able to get a valid response and have the Ping-o-Matic service ping all the services for you. Of course if you run that script too often, they’ll probably reject your request and tell you to slow down.

Getting WordPress.com Stats Helper to work with JetPack

WordPress recently released their JetPack plugin which replaced WordPress.com Stats plugin. After upgrading, things took a bit to update, but most things worked fine afterwards. However, my Popular Posts widget (Most Viewed Posts) remained broken with the error: WordPress.com stats not installed

I was too busy this week to look into the cause of the issue and a quick search online didn’t result in much traction. This weekend, I finally got some time and found this post on WordPress forums: [Hack] How to use Stats Helper beside Jetpack.

Thanks to herophuong’s help, I realized what was causing the issue. The Most Viewed Posts widget is part of the WordPress.com Stats Helper plugin and it was looking up a function that no longer exists. When you installed JetPack, it automatically deactivated WordPress.com Stats. The helper plugin needed the API key in order to query for data.

You can either do what herophuong suggested in his post, but I thought a more elegant solution would be to create a separate plugin that created the function that it was looking for. That way when there are updates to the plugin, it won’t trample over your changes.

Stats API Key

All this plugin does is add the missing function stats_get_api_key and return the API key. After you download this plugin, you have to add your API key to the plugin.

You probably have an API key already. There are a couple ways to retrieve it.

  • apikey.wordpress.com
  • Akistmet settings – Go into your WordPress Plugins page and click on settings underneath Akismet

You can always go to apikey.wordpress.com to sign up for one if you don’t have one already.

After you download the plugin and obtain your API key, open the included stats-api-key.php and replace ENTER_API_KEY_HERE with your API key. Remember to leave the quotes.

Upload this file to your WordPress plugins directory (usually wp-content/plugins/) and go back to your WordPress Plugins Page, refresh the page if needed, locate the newly added Stats API Key plugin, and activate it.

Your Most Viewed Posts and any other thing that relies on WordPress.com Stats Helper should work now.

A couple things I noticed was it took a couple minutes for it to successfully retrieve the stats. Before it completed that, my most viewed posts widget would show my latest 5 posts. But be patient and it’ll eventually work.

Traffic Spike on 5/9

So I was just checking my blog stats and I noticed a giant spike on 5/9, something that I had not anticipated:

Krunk4Ever! Stats

Going through my other stats, it turns out a particular set of search terms hit my blog that day:

5/9 Search Terms

Guess people were really looking for Mother’s Day comics. My guess is they landed on this Calvin and Hobbes strip.

I hope everyone remembered to wish their mom a happy mom day 🙂

Google Gears + WordPress

WordPress 2.6 first introduced the Turbo button I believe which allowed it to work with Google Gears. I never really thought much about it, but recently I had to modify a bunch of posts on my HD-Trailers.net blog and lets just say things weren’t as snappy as I’d like them to be. Updates took about 10-20 seconds. However I finally decide to try out the Turbo button and see where I could go from there.

Apparently what it does is that it downloads a bunch of static pages, so it doesn’t have to fetch them every time I load any of the editor/admin pages. And I do have to say things are now much snappier. It’s not lightning fast, but lets just say that post updates take less than 5 seconds. One would usually attribute these type of slowness to the webhost involved, but I’m seeing similar slowness on both Dreamhost and 1&1. However, after installing Google Gears and enabling it for both my blogs, things have been very smooth and I’m a much happier camper.

I also hate using my mouse to navigate the WordPress UI and wish they would implement some more keyboard shortcuts/hot keys. I did learn a new one. Apparently alt+shift+p publishes or updates a published post. For more WordPress keyboard shortcuts, take a look at The Ultimate List of WordPress Keyboard Shortcuts.

A few shortcuts I would really like are:

  • Save Draft
  • Preview
  • Jump to a specific section (i.e. Editor, Title, Tags, Categories, etc.)

Finally, I searched around to see what other stuff I use supported Google Gears. I couldn’t find anything officially posted by Google, but Wikipedia had this list:

There are a number of web applications that use Gears. These applications come from a variety of companies, including Google (Gmail, YouTube, Docs, Reader, Picasa for mobile, Calendar), MySpace (Mail Search), Zoho (Writer, Mail), Remember The Milk, and Buxfer. WordPress 2.6 added support for Gears, to speed up the administrative interface and reduce server hits.

The only things I use on that list other than WordPress are Google Reader and YouTube. Gears allows Reader to work offline, which is neat I guess, but half the stuff in my RSS feeds requires me to visit the site. For YouTube, I believe Gears support was only added to the uploader and <sarcasm>you know how big a video uploader I am on YouTube</sarcasm>.

Why Twitter? Why blog?

As you may have noticed, I haven’t blogged much recently. In fact, the only blog posts in the past 2 weeks were my twitter aggregations.

Earlier today, a friend tells me he doesn’t fully understand why people use twitter and he really doesn’t understand why people post tweets on their blog.

First and foremost, I’d like to point out there are many different reasons why people use Twitter. Initially, I thought it was rather stupid too. Who in the right mind would want to tweet about what they’re doing all the time? See Penny Arcade!’s Le Twittre. And who in their right mind would care about knowing every mundane detail of what’s going on in your life? Unless of course you’re a stalker, but then you wouldn’t be in your right mind anyway. However, I found a very different use for Twitter. I’ve only been using it for about 2 months, and I’ve been slowly adapting on how I would like to use it.

My current twitter usage pattern is something similar to my Random Crap posts, in what I like to call mini-blog posts. I hear about something I want to share or I see a link I want to share. I used to aggregate this into 1 giant random crap post and then post it. Otherwise, I would create a draft in hopes one day when I have time, I would expand on it. Looking at my drafts count of 46, I don’t think it’s likely most of them will ever be published.

Initially, I kept my blog and Twitter separate, however I soon realized that people who come to read my blog are people who want to see what I’m up to or what I find interesting, basically the same stuff I’ve been posting on Twitter, but in short bursts of 140 characters or less. Therefore, my new weekly Twitter digest posts are akin to my Random Crap posts, containing links and whatever else I wanted to share during the week.

I understand that my twitter also includes response/replies that readers may not care about. Then again, probably half the stuff I ever post here you wouldn’t care about, so I don’t think that would make much a difference. It’s also not uncommon for me or others to respond to someone specifically in a blog post. In fact, this blog post by itself can be considered a response to the statement posed to me earlier today. Besides, most of the response/replies included are stuff I do want to share, or else I would’ve used the Direct Message feature in Twitter.

Of course, there’s also nothing stopping you from skipping/ignoring my twitter posts if you don’t fancy them.