When it comes to giving Shopp products or categories a special treatment you’ll need to create templates that identify which store object you want to dress up. Using database IDs is straightforward, but here are some reasons why you might be better off using slugs.

Best Practices: Using Slugs instead of IDs

Best Practices: Using Slugs instead of IDs

Throughout Shopp it is often possible to use either a slug or an ID to identify objects such as products or categories. In this tutorial we are going to consider why one might choose a slug in preference to an ID (or indeed the opposite) to help ensure you make the right choice when customizing and working with Shopp.

Before we begin, let’s quickly remind ourselves about what IDs and slugs are and how they are used.

IDs

Almost everything in Shopp – including products, prices and images – is stored in a database and numerical IDs are used to identify each item. Generally speaking end-users such as customers and even site admins are not directly exposed to IDs and may even be completely unaware of them.

You can however figure out the ID for many things with a little observation. Try editing an existing product and look at the URL in your browser’s address bar – it should look something like:

http://mysite.com/wp-admin/admin.php?page=shopp-products&id=179

The product ID in this case, of course, would be 179.

Slugs

Slugs are a “URL friendly” version of the product or category name. They are typically lowercase, contain no special characters and hyphens are used in place of spaces. For instance a product called 5oz Modelling Hammer might have the following slug:

5oz-modelling-hammer

This is typically the final section of the product (or category) URL and is conveniently displayed in the product editor, right below the product title, like this:

Screenshot showing the URL preview (and slug highlighted in yellow) for a product in the Shopp product editor

What to use and when

Slugs and IDs can both be used in shortcodes as well as template tags to reference an object such as a product. The catalog-product shortcode for example can be used to insert a product inside a regular page or post. Here’s an example using an ID:

[catalog-product id="179"]

And you can substitute the product slug like so:

[catalog-product slug="5oz-modelling-hammer"]

Both are referencing the same product and the same basic principle applies to many of Shopp’s template tags, too. So which should you use – IDs or slugs?

The short and easy answer is if in doubt use slugs. The biggest reason for this is that if there comes a time when you have to migrate to a new WordPress installation or a new server (and that happens in the lifetime of many sites) then it is entirely possible for the IDs to change – and, all of a sudden, any shortcodes or custom template work relying on IDs will become invalid.

Slugs, on the other hand, are likely to remain the same in all but a few exceptional cases.

Is there a case for IDs?

Yes, of course: IDs are perfectly valid so long as you understand the risk – or else it’s something you can tolerate because you only have a handful of products. In fact, using IDs leads to better performance. However, please do bear in mind that the desire to squeeze every last drop of performance out of your site does need to be set against possible hair loss when/if you need to manually replace hundreds of IDs with slugs.

Last but not least, I should highlight that in most cases where you can identify items with a slug or ID, you can also use the actual name. My recommendation however would be to stick to slugs – names have pitfalls of their own surrounding the use of special characters (this doesn’t just apply to umlauts and cedillas – but some common forms of punctuation like apostrophes, too) which can also lead to confusion and even more possible hair loss.

I hope that gives a good overview of why we consider the use of slugs over IDs to be a best practice – and if you have any further questions or notes on this then please feel free to leave a comment and I’ll do my best to get back to you. Thanks!

Avatar of Barry

By

Barry Hughes is an independent web developer specializing in PHP and WordPress. When he isn't putting together plugins or baking up other tasty morsels of code he tries his best to update his blog at FreshlyBakedWebsites.net.

You must be logged in to post a comment.

© Ingenesis Limited. Shopp™ is a registered trademark of Ingenesis Limited.

Skip to toolbar