Shopping Cart (0)

Using Text Expander to speed up your theme development workflow

0 Posted January 17, 2012 by Rockstar Frog Categories: Code Snippets

As developers, creating a WordPress theme can often become a copy-and-paste affair. It’s modular architecture means that you’ll often reuse large chunks of code with just a small tweak here and there to suit the custom theme you’re developing.

A tool that has proven to be very helpful to many in doing this, is Text Expander by Smile Software. In this article, we’re going to cover a few handy WordPress code snippets that can be generated using this software in under 3 seconds.

Gets and Includes

WordPress recently encouraged developers to use their get_template_part() function instead of the native PHP file retrieval functions like include(), require() or require_once(). The WordPress function gets the file, but it includes an if_exists() conditional that won’t break your site if the file isn’t found.

The code snippet looks as follows:

get_template_part('includes/some_file_name');

So in the course of developing a theme, this is a function you’ll end up using quite often. Using Text Expander, you could create a snippet that looks as follows:

get_template_part('%|');

Note the “%|”? That’s a place holder that indicates where the cursor should move to once the snippet has been expanded. So for example, if you use “gget” as your abbreviation (the short bit of text that when typed, expands to the full snippet of code), then as soon as you type that, the get_template_part() function is expanded and your cursor is moved to the correct place in the snippet, ready for you to type.

In Practice – Creating a new Custom Post Type

Right, lets see it in practice. When creating a custom post type, there’s a standard chunk of code that is returned which contains a few changing values for things like the name of the post type, slugs and other various texts. Text Expander has a “fill in” feature which lets you enter a few variables which are used in the expanded code.

For example, this is the final code that is used to create a custom post type, along with a taxonomy:

add_action('init', 'myPostType_post_type');

function myPostType_post_type()
{
  $myPostType_labels = array(
        'name' => _x('Current Post Type', 'post type general name'),
        'singular_name' => _x('Post Type', 'post type singular name'),
        'add_new' => _x('Add Post Type', 'article'),
        'add_new_item' => __('Add New Post Type'),
        'edit_item' => __('Edit Post Type'),
        'new_item' => __('New Post Type'),
        'view_item' => __('View Post Type'),
        'search_items' => __('Search Post Types'),
        'not_found' =>  __('No Post Types found'),
        'not_found_in_trash' => __('No Post Types found in Trash'),
        'parent_item_colon' => '',
        'menu_name' => 'My Post Type'

  );
  $myPostType_settings = array(
        'labels' => $myPostType_labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => '100',
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','author','thumbnail','comments'),
        'menu_icon' => get_bloginfo('template_directory').'/images/icon-myPostType.png'
  );
  register_post_type('myPostType',$myPostType_settings);

};

register_taxonomy( 'my-taxonomy', 'myPostType', array( 'hierarchical' => true, 'label' => 'My Taxonomy', 'query_var' => true, 'rewrite' => true ) );

Whilst the bulk of that code never changes, things like the name of the post type, the labels and the function names are custom to each new post type. So the Text Expander snippet with all the required fill-in’s would look like this:

add_action('init', '%fill:namelower%_post_type');

function %fill:namelower%_post_type()
{
  $%fill:namelower%_labels = array(
        'name' => _x('Current %fill:namesingle%', 'post type general name'),
        'singular_name' => _x('%fill:namesingle%', 'post type singular name'),
        'add_new' => _x('Add %fill:namesingle%', 'article'),
        'add_new_item' => __('Add New %fill:namesingle%'),
        'edit_item' => __('Edit %fill:namesingle%'),
        'new_item' => __('New %fill:namesingle%'),
        'view_item' => __('View %fill:namesingle%'),
        'search_items' => __('Search %fill:namesingle%s'),
        'not_found' =>  __('No %fill:namesingle%s found'),
        'not_found_in_trash' => __('No %fill:namesingle%s found in Trash'),
        'parent_item_colon' => '',
        'menu_name' => '%|'

  );
  $%fill:namelower%_settings = array(
        'labels' => $%fill:namelower%_labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => '100',
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','author','thumbnail','comments'),
        'menu_icon' => get_bloginfo('template_directory').'/images/icon-%fill:namedash%.png'
  );
  register_post_type('%fill:namedash%',$%fill:namelower%_settings);

};

register_taxonomy( 'my-taxonomy', '%fill:namedash%', array( 'hierarchical' => true, 'label' => 'My Taxonomy', 'query_var' => true, 'rewrite' => true ) );

Notice the various occurances of “%fill:[VARIABLE]%”. When I type my abbreviation for this code snippet – ‘pposttype’ – Text Expander pops up a dialog that lets me type in my variables to be used in the expansion of this snippet:

Note that all the variables with the same name are duplicated, so you only need to type each one in once.

Once I’ve tabbed through the various inputs and I’m ready, I hit enter and the entire code snippet is expanded with the custom variables in place. All I need to do is save the file, require it from my functions.php file and presto – my custom post type is ready to be used.

Best Practices

Often times you’ll find that you’ll want to return the same function, sometimes in an environment where PHP tags are all ready open, and other times not – for example when displaying a snippet in your functions.php file VS displaying the snippet in your sites HTML.

Or, you’ll have 2 functions that are related – 1 snippet creates or registers the function (for example registering a sidebar or menu) and the other snippet echo’s the front-end part of the code. In such a case you might want to use an underscore to prefix the snippets are are the registrars of the function, and the snippet without an underscore to display the front-end snippet.

Through use however, you’ll discover a workflow that’ll work best for you.

Freebies!

We asked the kind folk at Smile Software if they’d be keen to give our readers a discount on the Text Expander software and they were kind enough to offer you a 20% discount! You can click on this link or simply use the code ‘EFROG112‘ when you checkout.

Coupon is valid until 1 February 2012 and applies to full licenses, Family Packs and Office Packs.

Thanks very much to Smile Software!

Text Expander has become invalueable in my workflow. Granted some development programs like Code come with this kind of snippet expansion system built right in, but the added benefit of something like Text Expander is that its a global system that can be used in any program, in any environment.

We’d love to know how you’ve used TextExpander in your WordPress theme development. Let us know!

Comments

Leave a Comment

George has something new to show you

Hello tadpoles! We're as happy and excited as a dog with two tails, to tell you about the latest theme from the team around the pond. It's called Cohesion, and we like to think of it as "a socially-engaged business theme". Read more

Valentines Giveaway: Win a copy of our new gorgeous theme – Occasions

Love is in the air, and lucky for you frogs aren't immune to Cupid's m... Read more

5 of George’s favorite WordPress plugins

Hello, tadpoles! Last week we released a global update to all our t... Read more

And the winners are….

Helloo, tadpoles! It's been a good Friday here at the pond, and it'... Read more

Twitter tweet and Facebook like. Magic.

Sharing is caring, right tadpoles? So today, we're going to show yo... Read more

eFrog News Categories

Browse our themes