Tag Archives: wordpress

Post Thumbnails only for Custom Post Types

26 Aug

The guys over at getshopped.org are working on a pretty major revision to their WP e-Commerce plugin. Part of this is migrating “products” into custom post types, including using post thumbnails for the product images. One of the problems that has cropped up in testing was where user’s themes didn’t support post thumbnails. The first fix for was for the plugin to force “theme support” for post thumbnails by calling

add_theme_support( 'post-thumbnails' );

This worked, in that it solved the problem – all of the post-thumbnail function calls worked. However, it left a bug, and one that isn’t a software, or a techie bug – it was the worst kind – a bad user experience. Suddenly “posts” and “pages” would allow you to set featured images when authoring, or editing, but because the theme didn’t really support post thumbnails, those images would never show leaving a confused, bewildered user. So, the solution is to enable the support in general, but disable it for specific post types:

function check_thumbnail_support() {
 if (!current_theme_supports('post-thumbnails')) {
   add_theme_support( 'post-thumbnails' );
   add_action('init','remove_posttype_thumbnail_support');
 }
}
add_action('after_setup_theme','check_thumbnail_support',99);

function remove_posttype_thumbnail_support() {
 remove_post_type_support('post','thumbnail');
 remove_post_type_support('page','thumbnail');
}

This does three important things:
1. Check to see if the theme supports thumbnails already – if so, do nothing
2. If not, then turn on post thumbnail support, but also …
3. Remove support from “post” and “page”

The “gotcha” if you’re trying to come up with this yourself is that theme support is for “post-thumbnails”, but post_type_support is for the more generic “thumbnails”. Hope this comes in handy some someone.

WordCamp UK 2010

23 Jun

WordCamp UK is an informal annual gathering of WordPress publishers, designers and developers based in the United Kingdom.

This year’s event is being held in Manchester (Which is nice and central if you ask me!) and looks like it’ll be a great chance to chat about what’s new and groovy with WordPress, and find out who’s doing what in the UK.

You can find out more at the WordCamp UK site.

Reverse the product order in WP e-Commerce

9 Jun

While WP e-Commerce lets you choose how to order your products (E.g. Price, Name, Date Uploaded) through the admin area, it doesn’t let you control whether you’re sorting ascending or descending – e.g. cheapest first, or most expensive first.

Here’s a a quick tip to show how to control the sort order, just add the following snippet to your WordPress theme’s functions.php file:

<?php
    if (!isset($_SESSION['wpsc_product_order'])) {
        $_SESSION['wpsc_product_order'] = "DESC";
 }
 ?>

Just swap DESC for ASC to swap the default order. Happy sorting …

Adding Post Thumbnails To Your Sitemap

17 Apr

Google recently blogged about Adding Images to your Sitemaps. When I read this my thoughts jumped straight to a friend’s site that is image-orientated, and could really benefit from this. His site uses WordPress’ post thumbnails to store an image against each post, and I wanted to get those images into his sitemap.

His site already uses the great Google XML Sitemaps plugin to auto-generate an XML sitemap, so I thought I’d have a go at extending it to support post thumbnails.

The resulting XML looks a little like this:

<url>
  <loc>http://test.leewillis.co.uk/hello-world/</loc>
  <image:image>
    <image:loc>http://test.leewillis.co.uk/wp-content/uploads/test.jpg</image:loc>
    <image:title>Hello world!</image:title>
    <image:license>http://test.leewillis.co.uk/license/</image:license>
  </image:image>
  <lastmod>2010-04-14T21:15:15+00:00</lastmod>
  <changefreq>monthly</changefreq>
  <priority>1.0</priority>
</url>

Some notes on the above. The image title is set the same as the post’s title – which I figure should be a reasonable result, from a user and SEO point of view. The license URL is configurable (One per site).

I’ve submitted the code to the plugin author to hopefully get included, but if you can’t wait, there’s a patch file available here – just apply it with the “patch” utility.

Region-based shipping with WP e-Commerce

9 Apr

I’ve been asked a few times about how to split up countries into regions and charge shipping differently to the different areas in WP E-Commerce. This was something that was almost, but not quite possible with one of my existing plugins.

Now it’s just a little bit easier.

What You’ll Need

In this example we’re going to show how to achieve the following:

  • Set of Weight rates for New Zealand (North Island)
  • Set of Weight rates for New Zealand (North Island)
  • Set of Weight rates for the rest of Asia/Pacific
  • Set of Weight rates for Europe

The Plan

To acheive our end goal we’re going to:

  1. Create 2 regions attached to the country “New Zealand”
  2. Remove New Zealand from the Asia and Pacific continent (So people ordering to NZ see those prices, not the general “Asia and Pacific” pricing
  3. Set up per-country/region rates for New Zealand
  4. Set up per-continent rates for Asia and Pacific, and Europe

Getting Started

The first job is to find out the “country ID” for New Zealand. Using your favourite database you’ll want to look in the wp_wpsc_currency_list table for the entry for New Zealand, and make a note of the value of the “id” field – in our case 156.

While we’re here, we’ll also move New Zealand out of it’s current continent. This basically just involves updating the same record, and setting the continent field to something unique, e.g.

Next – you’ll need to create whatever regions you want in the wp_wpsc_region_tax table, using the ID you’ve just found as the “country_id” field, e.g.

Configuring your pricing

That’s all with the scary database stuff, now on to the easy stuff. First of all you’ll need to activate both of the shipping modules:

First up we’ll configure the continent-based pricing. Click on “Edit” next to the weight-continent pricing. Up will pop the configuration pane, with a drop-down of the standard continents – and also our “newzealand” continent – which we’re not going to use since New Zealand is going to be priced by region.

Let’s pick Europe, and configure some weight rates.

In this case we’ve just configured one weight band – which means that anything shipped to Europe will be charged at the same rate – £80 in this case.

We can do the same for Asia and Pacific, except maybe setting up multiple layers using the “Add Layer” link.

At this stage shipping will work for Europe, Asia and Pacific, but not for New Zealand (Since we took it out of Asia and Pacific).

Click on “Edit” next to the Weight / Country and Region pricing. Up will pop the configuration pane, with a drop-down of the countries, including regions where configured – for example, we can see our North Island, and South Island regions here:

Selecting a region gives us the chance to configure weight rates, e.g.

Repeat for other regions, and you’re set. Happy Shipping!

Note: All weights should be set up in lbs, no matter what weight unit you used to set the weight for your products. WP e-Commerce will convert between them for you.

WordPress 2.9 e-Commerce

6 Mar

WordPress 2.9 E-Commerce WordPress 2.9 e-Commerce, has been published – the definitive guide to setting up an e-commerce site on your WordPress blog using the WP e-Commerce plugin, of which I’m a bit of a fan.

This book provides an easy to understand, step-by-step approach to installing, configuring, and using WP e-Commerce to run your online store. We will construct an example store in the book with enough information and flexibility to adapt the store to your specific needs.

This book is for you, if you are interested in using WordPress as the basis for a store that can sell physical items, downloads, or services. It is ideal for a sole proprietor or small business owner with only basic, in-house technical skills. Some prior knowledge of WordPress will help, but is not required. No knowledge of PHP is expected, but it will be helpful.

You can buy it from Amazon here.

Disclaimer: I’ve linked to Amazon using affiliate links, to help fund this blog.

Getting URL of Post Thumbnails in WordPress 2.9

29 Jan

One of the improvements in WordPress 2.9 is native support for thumbnail images on posts. The API is pretty good – although it assumes that you want to return the whole IMG tag. In my case I wanted to return the URL of the image (So I could wrap the actual thumbnail in a lightbox-style link to the larger versions).

So – if you want to get the URL of your posts thumbnail – here’s the code you need:

$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];

I hope this helps you with your theme development.

How to Split Test WordPress Themes

17 Oct

I recently re-worked a site for a client. He was moving over from Joomla to WordPress, so I started from scratch with his theme design. They liked the new theme I came up with – however I’d put the menu and other navigation (Links to recent articles, etc) in a right hand sidebar, and they weren’t too sure about that.

“Doesn’t everyone have the menu on the left?”

Now I’m no usability expert, and while I figured they were probably right I thought we’d run a little experiment. I trawled around a bit, and I found a nice little post by David Dellanave. While I was OK implementing David’s solution I though it was probably a little complicated for some of my other clients, especially when it came to running 3 Google analytics accounts – so I did some digging.

The result is the SES Split Testing plugin.

To use the plugin you’ll need to be using Google Analytics tracking on your WordPress blog already (If you’re not, then I’d recommend the “Google Analytics for WordPress” plugin), then :

  • Create two (or more) versions of your theme
  • Select them in the plugin settings
  • Define custom segments in Google Analytics
  • Sit back and watch the results roll in

Create theme variations

In most cases, you’ll want to test two versions of 1 theme with some slight changes. Let’s assume your theme is in the folder wp-content/themes/mytheme. Take a copy of your theme and put it in fresh folder, e.g. wp-content/themes/mytheme_2. Open up the style.css file in the second folder, and change the Theme Name, e.g.

/*
Theme Name: MyTheme
*/

to

/*
Theme Name: MyTheme (v2)
*/

Set up plugin settings

Wordpress Theme Split TestingNow you can drop the plugin into your plugin directory, activate it in the WordPress backend, and then go to the settings page (Settings->SES Theme Split Test). The plugin will list all of your available themes, and you can choose which ones you want to be part of the test.

Once you’ve saved the settings, the split testing will kick in. Visitors will be randomly assigned one of the chosen themes, and they’ll receive that theme any time they visit in the next 30 days. Not only that, but their visit will be tagged with a special variable that you can pick up on in Google Analytics.

Set up Custom Analytics Segments

Google Analytics lets you define custom segments (Ways of grouping customers). In this example we want to group customers according to which theme they were shown and then we can measure our chosen metric (Purchase / sign-up / page views – whatever) for the two groups and compare them. To get started, you probably want to wait 24 hours so that Google starts to receive the tagging data.

Setting up custom segmentsLog into your analytics account, and click on the “All Visits” dropdown (Top-right, just above the date selector), and then choose “Create a new advanced segment”. Then, you want to expand the “Visitors” dropdown under “Dimensions”

Custom Segment Build

Scroll down until you find “User Defined Value” and drag it onto the right hand box that says “dimension or metric”. If Analytics has got your tagging data (See – I told you to leave it 24 hours!) then you’ll get to choose from the values being passed, e.g.

Analytics Custom Segments

Create a segment for each of your themes, and give them a meaningful name, then you’ll be able to select them as custom segments in your reports.

Segmented Analytics Reports

Now it’s just time to wait, see what happens, then decide. Once you’ve got a winner then just set that as your blog’s standard theme in the normal way, and either deactivate the plugin, or unselect the themes in the settings and users will see the blog default.

How to write an application for WordPress

7 Sep

I’ve recently been doing some work for a family member on a WordPress blog they run, and the need came up to build a mini-app. Now historically, I’m a Joomla kind of guy, and it’s fairly clear that for a Joomla site you would build a component to implement your app.

However, in WordPress it’s a little different. I’ve found loads of articles about available API calls, manipulating “the loop” and all sorts of useful information. What I haven’t found though is something that explains at a fundamental level how I should approach the problem.

There isn’t a concept of an “application” under WordPress, the only items that are accessible are “posts” and “pages”. As far as I can tell, I have two options:

  1. Publish a post/page with no content other than a special string (E.g. [my-app]) and then write a plugin that filters post content and replaces the magic string with the output from the application
  2. Create a page, and assign it a custom template. Implement your application in that custom template

Option 1

It works, I know because this is what I did :)    However there is a performance penalty, your application code is called on every post, and so you have to take steps to make sure it doesn’t run unnecessarily. The other disadvantage is that well, it just doesn’t seem clean.

Option 2

This seems a little nicer, but doesn’t really fit with WordPress’ nice plugin installation architecture – as far as I can tell plugins can’t auto-install into the current theme directory, and custom templates can’t live anywhere else.

So, this leaves me a little confused – can anyone point me as to how I should have solved this problem – or does WordPress need to grow up a little and add “application” alongside posts and pages?

Tracking Twitter Traffic Redux

7 Aug

I wrote a previous post about how you could hack the excellent Twitter Tools plugin from Alex King to add Google Analytics tracking variables to the URLs posted to twitter when you posted a new blog post.

I submitted the patch upstream, and Alex kindly pointed out that rather than bodging around in his code – you could achieve the same effect with a filter.

So, here it is. Simply download this file and save it in your wp-content/plugins directory. Activate the plugin in your WordPress backend (It will be listed as “Twitter Tools Tagger”) and away you go. Whenever the twitter-tools plugin tweets about one of your blog posts, the URL will be tagged with analytics tracking.

As an added bonus, version 2.0 of Alex’s plugin can also pump your URLs through the http://bit.ly/ URL shortening service so your URLs will be tagged, and shortened.

So, that’s nice.