Keith Devon

Menu

blog

The Tools of the (WordPress Freelance Development) Trade

I’ve been freelancing for a while now. I’ve also been developing WordPress sites for a while. In that time I’ve used many different tools. Some have been great, and some less great. Here I share a list of the tools that I’m currently using:

Accounting

I use the excellent FreeAgent* to keep track of my estimates, invoices, expenses, bills, tax and more. FreeAgent actually makes filing expenses, dare I say, fun!

You can get valuable overviews of your business, so that you can finally feel in control. To check out FreeAgent click here*.

* Those are affiliate links, but guess what, if you follow those links, not only do I save some cash, you do too!

Time tracking

I’ve been using Toggl for years now. It’s a really simple time tracking tool with some clever features (here’s a good review: https://thefreelanceeffect.com/toggl-review/). It also integrates with my favourite project management tool…

Project management

The mighty Basecamp has been an industry standard for years now. I haven’t updated to the ‘new Basecamp’ yet, I find that Basecamp ‘classic’ still does everything that I need it to, with all the integrations that I need.

Design

Wireframing

I’ve faffed about for too long trying to keep up with free wireframing tools. So, I’ve just signed up to Mockingbird, which is an online wireframing tool. It’s not free, I pay $9/month for the ‘Personal’ plan. There might be better ones out there, but this does the trick for me.

Graphics

I’ve been using Adobe’s Photoshop for years. It’s probably time that I upgraded though, I’m still on CS3.

Web development

Text editor

Coda, by Panic, has been around for ages. I started using it because Chris Coyier did and I’ve stuck with it. There are some nice features beyond what you would expect (including a WordPress plugin for code completion and syntax highlighting). I hear that there is even more fun to be had with Coda2.

Local development environment

I’m on a Mac, so I started off using MAMP. MAMP makes it pretty easy to set up and run a local Apache, MySQL and PHP stack. This allows you to install websites on your machine, making development much quicker and safer. If you aren’t doing this already, do it now!

I’ve since upgraded to MAMP Pro, which allows me to easily create local domain names. So, instead of using localhost/mysite.com, I can use kdev.mysite.com (or anything else for that matter!). MAMP Pro also allows you to access your local server remotely, although I haven’t tried that yet.

FTP client

FileZilla is my FTP client of choice. I can’t compare it to others, but it’s always done the job for me. It’s free.

Browser

If you’re not using Google Chrome, chances are you should be. I’ve found it to be quicker than Firefox, and I prefer the developer tools to Firebug.

 

I hope that helps. If you have any thoughts or suggestions, I’d love to hear them in the comments.

WordPress Security

Since I listened to Kieran O’Shea’s talk at WordCampUK, I’ve been taking security much more seriously. Kieran had some pretty scary statistics regarding hacking. For example, 90% of all businesses have been hacking victims in the last 12 months, and, there is an average of 156 days before victims realise that hacking has taken place.

Having your site hacked is bad news. There are many ways in which a site can be hacked, and many outcomes, but be assured that none of them are pleasant.

Luckily there are things that you , as WordPress site owners, can do about it.

User names and passwords

This is my absolute number one tip, use strong username and password combinations.

I recently had my PayPal account hacked because I used the same username and password everywhere. Eventually, I either signed up to a fake site, or a site I registered with was hacked. Either way, once the hackers had my details they could access everything of mine online.

Here are some rules to follow:

  1. Don’t use ‘admin’ as a username.
  2. Use a strong password.
  3. Use a unique password.

If you have a user called ‘admin’ set up a new user with administration privileges and then go and delete the ‘admin’ user. Do it now!

WordPress will tell you if your password is strong or not. If it isn’t, think of another one.

Since my PayPal incident I’ve started to use unique passwords for nearly every site that I use. I know that it sounds daunting, but there are ways to manage passwords (check out 1Password).

WordPress updates

Keeping up to date with the latest version of WordPress and especially the security patches is my next biggest tip.

As of writing WordPress is at version 3.4.1. They are constantly releasing security patches and other major upgrades. Keep ahead of the hackers by having the latest version installed.

Warning: back up your site fully before installing updates, especially point releases, i.e. moving from 3.4.1 to 3.5.

Sometimes upgrades will break your themes or plugins. This is just the way it is. Nobody can predict the future, but the best themes and plugins should be more future proof.

If you run into problems with upgrades, revert to your backed up version and contact your WordPress professional. Ideally, have your pro do the upgrade, testing and bug fixing for you.

I will be offering my clients upgrade services. Please contact me to discuss upgrading your site.

Helpful plugins

There are some really helpful security plugins on the market:

Security

Backups

If you need help or advice regarding security please get in touch.

Custom Post Types

Last night I gave a presentation at the WordPress London meetup. I talked about custom post types in WordPress, something that has really excited me since WordPress 3. I’ve included the video, a rough transcript of the presentation and the slides at the bottom.

What we’ll cover

  • What are custom post types
  • When to use them
  • How to use custom post types
  • Taking things further
  • Resources

What are custom post types?

I’ve searched high and low on the internet for a decent definition of custom post types, with no luck. The best that I’ve seen is that they are really custom content types.

Replacing the word ‘posts’ with ‘content’ gives us a much better understanding of what custom post types are all about. The idea is that there is some content on your website that might not fit neatly into the typical page’ or ‘post’ mould. Think of the following examples:

  • Movies
  • Staff
  • Podcasts
  • Books
  • Products
  • Portfolio items
  • Testimonials, etc, etc.

Then why are they called custom post types?

Within WordPress there are already various post types: posts, pages, menus and revisions. With version 2.9 came the ability to define your own, ‘custom’, post type. These are stored in the wp_posts table. Hence custom post types.

When to use custom post types

Here is a fairly typical client request:

“I want to create a website for my holiday home rentals company. I have a portfolio of 20 properties that I want to be able to manage and update. Each property has the following information:

  • Name
  • Address
  • Short intro
  • Full property description
  • An image
  • Price per week
  • Number of rooms

I would like the user to able to sort the properties by ‘Price’ and ‘Number of rooms. I will also be blogging on the site.”

Without custom post types

In those dark days before WP 2.9 I probably would have used posts for the properties. I then would have created a category of ‘properties’ so that I could style those property posts differently than the regular blog posts. I then would have used custom fields for the meta data – rooms and price.

This used to work, but it was an ugly solution, a ‘hack’. The property posts would be mixed in with the regular blog posts both in the admin and the front-end.

Enter, custom post types

With custom post types we can separate our content into logical groups. Posts are now just chronological blog/news items again and properties have their own section.

This also makes theme development easier. The posts and properties are no longer lumped together, so no need to hack the index.php loop. New templates are available specifically for your properties post type too.

Basically, using custom post types is going to seriously improve workflow for both you and your client.

How to use custom post types

Creating your custom post type

Before I start, I have to tell you that there are ways to create custom post types on your WordPress site without having to touch a single line of code. There are several plugins and online code generators that will do it all for you. However, I strongly encourage you to have a go at this yourself. Even if this is the first bit of theme development that you have done, give it a go. You’ll have a much better idea of what’s going on and you’ll find it so satisfying.

Open up your theme’s functions.php file using your favourite text editor, and add the following:


add_action( 'init', 'create_my_post_types' );

function create_my_post_types() {
register_post_type( 'kdev_properties',
array(
'labels' => array(
'name' => __( 'Properties' ),
'singular_name' => __( 'Property' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title','editor','excerpt','thumbnail'),
'rewrite' => array( 'slug' => 'property' ),
'register_meta_box_cb' => 'add_property_metaboxes',

)
);
}

This code is all we need to get custom post types working on our WordPress site. in fact, it’s more than we need, I’ve added a few lines, 11, 13 and 14, that aren’t essential but useful for what we’re trying to achieve.

Line 4 is where the magic starts, we’re registering our post type and giving it a name. I’ve prefixed the name with ‘kdev_’ to avoid clashes with any other plugins.

Line 6 starts the ‘labels’ array and at this point we’re only using two labels ‘name’ and ‘singular_name’. These will appear in our theme instead of the ugly ‘kdev_properties’. There are lots more labelling options, check them out here http://codex.wordpress.org/Function_Reference/register_post_type#Arguments.

Line 10 is telling WordPress to make the post type public, that is visible, in the admin and front-end. Strangely this defaults to false, so you must include it if you want to see your post type.

‘has_archive’ on line 11 is optional. I’m using it to tell WordPress that I will be using a custom archive template.

The ‘supports’ array on line 12 is where it starts to get interesting. Everything that you are familiar with from posts and pages are available to you here. I’m just enabling the elements that we’ll need for our property post type.

Line 13 enables a URL rewrite. This changes our URL structure from kdev_properties/propertyname to property/propertyname (assuming we’re using a ‘pretty’ permalink structure).

The last line, 14, is telling WordPress that I wish to use a custom meta box on this post type. this will be explained in greater detail later on.

Now log in to your WordPress admin area and you should see something similar to the following:

You can see that our custom post type, ‘properties’, has been added to the main menu, keeping it separate from posts and pages.

If you click on the ‘Properties’ link and then ‘Add New’ you will get to the following screen.

Here we can see the ‘supports’ array in action. We’ve added some useful functionality here but it’s also important to note what we’ve left out. The categories and tags that we are familiar with from posts are gone, as are page attributes, revisions, comments and discussions. This leaves us with a nice clean UI. There are no extraneous admin elements to distract the user.

Our code hasn’t just created a great admin screen, it’s also enabled us to view our custom post type live on the site. From the ‘Edit Post’ screen, click on ‘Preview’ to see what your post will look like (add some content first for a better effect!). You’ll see something like this:

That’s great so far, but the chances are that you’ll want your custom post type to look slightly different to regular posts. WordPress gives us two template files to do this; single-[posttype].php and archive-[posttype].php.

The single template will let you style your individual custom post types, in our case the single property page. It’s often easiest just to create the new template file (e.g. single-kdev_properties.php), in your theme directory, and copy and paste the code from your theme’s single.php file. Then you can make any changes that you require. Before you do that though, there are some more tricks that will help you make your custom post type really come to life!

The archive-[posttype].php template will create a page that displays a list of your custom posts. Using the same idea as above, you can copy and paste from your index.php file and edit from there. Remember the ‘has_archive’ argument that we added? That line is telling WordPress to use our custom archive file.

Congratulations! You’ve created your first custom post type. That wasn’t too hard was it? There is loads more that we can do with our custom post type.

Taking things further

Remember the original client request? She wanted to be able to add ‘price’ and ‘number of rooms’ data to her properties. Of course, we could just add this to the main text editor, but then it would be tricky to separate the data if we wanted to use it by itself. This is where custom meta boxes come in.

Custom meta boxes

Here’s where custom post types start to get really cool. We’re not limited to the ‘title’, ‘excerpt’, etc. that WordPress provides us with. We can add new boxes to our edit screen that accept any kind of data that we can think of.

Here’s an example of some meta boxes:

The first meta box is a simple text field, the second a radio button. You could have checkboxes, text-areas, TinyMCEs, etc. By now your imagination should be kicking in to show you the possibilities available!

We want two meta boxes, one for price and one for rooms. Here’s the code that we need to achieve this:


//Add custom meta box

// Add the Properties Meta Boxes

function add_property_metaboxes() {
add_meta_box('kdev_properties_rooms', 'Number of Rooms', 'kdev_properties_rooms', 'kdev_properties', 'side', 'default');
add_meta_box('kdev_properties_price', 'Price per week', 'kdev_properties_price', 'kdev_properties', 'side', 'default');
}

// Output the Property metaboxes

function kdev_properties_rooms() {
global $post;

$prop_rooms = get_post_meta($post->ID, '_rooms', true);

echo '<label>Rooms</label><input class="widefat" name="_rooms" type="text" value="' . $prop_rooms . '" />';
}

function kdev_properties_price() {
global $post;

$prop_price = get_post_meta($post->ID, '_price', true);

echo '<label>Price per week</label><input class="widefat" name="_price" type="text" value="' . $prop_price . '" />';
}

// Save the Metabox Data

function kdev_save_property_meta($post_id, $post) {
if ( 'kdev_properties' == get_post_type() ) {

if ( !current_user_can( 'edit_post' , $post ->ID )) return $post ->ID;

$property_meta['_rooms'] = $_POST['_rooms'];
$property_meta['_price'] = $_POST['_price'];

foreach ($property_meta as $key => $value) { // Cycle through the $property_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
}

add_action('save_post', 'kdev_save_property_meta', 1, 2); // save the custom fields

This is a bit more complex and I won’t go through every line. Add this to your functions.php file and make sure that the variable names are correct (if you have used something other than ‘kdev_properties you’ll need to change it here too).

There are some arguments that you may wish to tinker with. Remember, you won’t learn if you don’t make mistakes, so don’t be scared. It’s always a good idea to make backups before you start experimenting though, especially with a live site.

Adding that code will give you something like this:

Check out those custom meta boxes on the right! Sweet.

As cool as it is to have custom meta boxes, they’re not going to do anything for you without a bit of work to your template files.

Open up one of your custom template files and add the following:


Price per week: £<!--?php echo get_post_meta(get_the_ID(), '_price', true); ?-->

Number of rooms: <!--?php echo get_post_meta(get_the_ID(), '_rooms', true); ?-->

The get_post_meta() function is creating all the magic here. It takes a few arguments, the post ID, the meta key, and whether to return a single string or an array (‘true’ will return the string).

I’ve added those lines to my custom archive template to get the following:

Nice work. Our custom post types have meta boxes and we have some pretty nifty templates. There’s one more part to the holy trinity of custom awesomeness…

Custom taxonomies

Taxonomies are ways to classify your data. If you’re familiar with WordPress you will already be using them; categories and tags. These are both taxonomies, but they are very general, and you’ll probably want to keep these for your blog posts.

For our custom post types we can create our own custom taxonomies. There are two main types; hierarchical and, erm, not.

Hierarchical taxonomies

Hierarchical taxonomies have the concept of parents and children, like categories. You can create tree like structures of nested classifications.

Non-hierarchical taxonomies

These are like post tags. They don’t have parents or children.

Creating a custom taxonomy

function my_register_taxonomies() {

register_taxonomy(
'area',
array( 'kdev_properties' ),
array(
'public' => true,
'labels' => array( 'name' => 'Area', 'singular_name' => 'Area' ),
'hierarchical' => true,
)
);
}

add_action( 'init', 'my_register_taxonomies' );

This is all we need to create a custom taxonomy for our custom post type. I’ve given it a name, assigned it to the ‘kdev_properties’ post type and given it some arguments. Again, have a play around. The code can be added to your functions.php file as before.

And so we have it. Well done for getting to the end!

Hopefully, you’ll be inspired to use custom post types on your next project. You’ll never look back, I promise.

Resources

http://codex.wordpress.org/Post_Types
http://codex.wordpress.org/Function_Reference/register_post_type

Plugins

http://wordpress.org/extend/plugins/custom-post-type-ui/
http://wordpress.org/extend/plugins/custom-content-type-manager/

Posts

http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress
http://devpress.com/blog/conditional-checks-for-custom-post-types/
http://css-tricks.com/forums/discussion/8538/wordpress-3.0-custom-post-types-and-meta-boxes/

Easy meta boxes with WPAlchemy

WPAlchemy MetaBox PHP Class

The slides

 

WordPress London meetup #5

The group continues to go from strength to strength with at least 30 members attending.

This month’s meetup included a talk from BuddyPress developer Paul Gibbs. He talks us through the new release, the epic version 1.5.

Paul’s blog post, with slides, can be found here: http://byotos.com/2011/09/27/buddypress-at-wordpress-london-220911/.

Then Jeff Ghazally shows us how easy it us to sell music online using WordPress and the WP E-commerce plugin.

We also had a great presentation from Shakur about the SOLR search plugin and how it can be used to improve the search performance and capabilities on your WordPress site. Unfortunately due to technical difficulties (an over-heated camera!) we didn’t get this one on video. Sorry Shaksi!

Beginner WordPress Theme Development

Last Thursday (28th July 2011) we held the third London WordPress meetup. The meetup is for WordPress users and professionals in London and aims to provide a networking and learning platform.

I gave a talk on Beginner WordPress Theme Development, which introduced the basic structure of a theme and some key concepts. Here are the presentation ‘slides’ and the video.

The Custom Image Size Problem

I’m building a WordPress theme where I use custom thumbnail sizes. I use the filter

add_image_size( 'Gallery list', 350, 200, true );

That works fine until I edit the original image in WordPress. For example, I’ve rotated a few of the images. When I click on the thumbnail it takes me to the now corrected image, however the thumbnail itself is as it was.

Do I need to regenerate these thumbnails? Is there a way to do this automatically after editing the image?

Thanks for your help.

Keith

Proper float clearing

I was browsing through Forrst recently and happened across the following advice on float clearing.

An easy way to properly clear floats without extra markup. If you’re not using some sort of clearfix you probably should be.

.clearself:before,
.clearself:after {
content: " ";
display: block;
height: 0;
overflow: hidden;
}
.clearself:after {clear: both;}
.clearself {zoom: 1;} /* IE < 8 */

This looks like a great way of reducing design related markup in your HTML. What do you think? Have you tried this technique before?

The original post is here.

20% off web design for artists

To celebrate the opening of this years Annual Exhibition application, I am offering 20% off web design services for artists. That means my daily rate is down from £175 to £140. This offer will last until the end of the summer and is first come first served.

Sites can be built from £400 depending on options. Contact me or fill out my online quote form to get started.

HTML Fractions

I wanted to use some pretty fractions in an article today. I don’t like the look of “1 1/2”, not very elegant.

Of course, the answer lies within HTML special characters. There are a few ways to achieve the same thing:

  • &frac14; will give you ¼. You can change the ‘1’ and ‘4’ to any other number. But hang on, what would &frac124; give you? The answer is ½4;. Hmm…
  • The more versatile solution is to use &frasl;. However, this won’t format properly without adding <sub> and <sup> like:

    <sup>1</sup>&frasl;<sub>10</sub>

    Which will output 110.

Boom! HTML fractions!

Drop down menus, z-index and IE

One of Internet Explorer’s (IE) most commonly encountered bug is when using z-index. I experienced this recently when using the DDSlider plugin for WordPress along with a drop down navigation.

Whilst doing some browser testing I noticed that the drop down menu hides behind the DDSlider. I tried to fix this using z-index. I applied positions and z-index to the offending elements but they still did not behave properly.

After Googling the issue I came across this post which explains the problem perfectly. I needed to apply z-indexes to higher level elements that sit side by side in the DOM. In my example I applied a position:relative; z-index:2 to the #header div and a position:relative; z-index:1 to the #main div. Seems to have done the trick perfectly!

Spinning Update progress on WordPress pages

Spinning Progress Wheel

One of my clients recently informed me that a site that I had built had a problem; when some pages were published or updated, the progress wheel would spin until the browser eventually timed out.

It had been a month or so since I had finished the site development and there had been lots of new content and plugins added. I turned on the WordPress debugging and got to work.

Finding the problem

There were a lot of errors being generated, mostly from plugins but also from WP itself. I also noticed a few memory limit issues which I fixed using the .htaccess file.

I got to work isolating the various components. All plugins, themes and content were tested to no avail. Next up I installed the site on different servers. I looked at the database for anomalies, contacted the hosting provider, nothing.

Lying in bed last night I couldn’t sleep, I had to give it another go. I did some more tests on what I thought was the offending plugin and associated database table. Still no joy. In one last ditch attempt I did various Google searches of the symptoms. I jumped from forum to forum and followed links all over the place.

Got it!

Then I found it. The custom permalink structure of “/%category%/%postname%/” that I have used on nearly every site was the culprit. Now, someone please correct me if I’m wrong, but I’m sure the WordPress codex used to give that exact structure as an example of how to implement “Pretty Permalinks”. Not any longer. Apparently that structure causes performance issues on sites with lots of pages. For every page a whole new set of rewrite rules are created until the system just can’t process it any more.

The solution is to use a non-verbose entry at the start. For example %year% or %month% or both are ideal but %category%, %postname%, %tag% and %author% should be avoided.

Fortunately, the codex has been updated to advise against this practice and the new example “pretty permalink” is /%year%/%postname%/.

Lessons learned

For most site you can probably get away with the offending structure. In fact many SEO and WordPress ‘gurus’ recommend it. Just be warned that if your site grows in terms of pages then you may encounter this problem in the future.

Some of the lessons that I’ll take with me:

  • Keep up to date with changes to WordPress documentation and best practices.
  • Clearly define who takes responsibility for WordPress faults in your project contract.
  • Never give up!