WordPress Custom Post Types Unveiled

In this tutorial, we will illustrate how to create a WordPress custom post type called My Things. I have narrowed the procedure down to 4 steps, which will be explained in depth. The most obvious place to insert the code is somewhere within your functions.php, but to add another level of organization, I recommend linking to a separate file. In my first attempts at creating WordPress custom post types, I reviewed numerous articles explaining how. Some are better than others, and a few were exceptionally helpful, yet none were the whole enchilada. So here is my attempt to document & share exactly what worked for me.

Outline

Below is a oversimplified outline of a wordpress custom post type. It is not meant for cutting & pasting, but rather to help familiarize you with the fundamental steps for preparing your custom post type. Also, it is good practice to prefix your IDs in order to make them more unique. In this case, we will be using mt_, as in My Things.

[code]
// Part I: Initialize & Register
add_action( ‘init’, ‘mythings_init’ );

function mythings_init() {
register_post_type(
‘mythings’, // $post_type — your post type slug
/* YOUR POST TYPE ARGUMENTS */
);
}
// Part II: Add Taxonomies
add_action( ‘init’, ‘create_mythings_taxonomies’ );

function create_mythings_taxonomies() {
register_taxonomy(
‘mt_groups’, // $taxonomy
‘mythings’, // $object_type — stick with $post_type for now
/* YOUR TAXONOMY ARGUMENTS */
);
}
// Part III: Add Metaboxes
add_action( ‘admin_init’, ‘mt_admin_init’ );

function mt_admin_init(){
add_meta_box(
/* SETUP YOUR META BOX ARGUMENTS HERE */
);

function mt_first_metabox() {
/* CREATE YOUR ADMIN FORM */
}
// Part IV: Save your Data
add_action(‘save_post’, ‘mt_save_data’);

function mt_save_data(){
/* YOUR FORM-SAVING CODE */
}
[/code]

And that’s it in a nutshell. Now we can swap in some usable code.

Part I: Basic

This will start the job off right, but in its simplest form. WordPress secretly provides us with a number of handy default arguments, which saves us a lot of real estate in this example.
This example differs slightly from that of WordPress Codex. It has been recommended on new2wp.com to save the register( ) function until after the init( ) function. I believe I have felt the adverse side effects of doing it the other way around.

[code]
// create your init() function
add_action( ‘init’, ‘mythings_init’ );

// use your init() function
function mythings_init() {
array(
‘labels’ => array(
‘name’ => __( ‘Things’ ), // general name in menu & admin page
‘singular_name’ => __( ‘Thing’ )
),
‘public’ => true,
‘has_archive’ => true,
);

// now register the post type
register_post_type( ‘mythings’, $args );
}
[/code]

Part I: Advanced

You will find yourself wanting to be much more specific, so this example includes most of the different parameters, and I’ve done my best to comment on their function. Since we have a lot more information to add, we will be as organized as possible by utilizing the custom variables $labels and $args.

[code]
// add your init() function
add_action( ‘init’, ‘mythings_init’ );
// use your init() function
function mythings_init() {
// setup an array called $labels to insert into ‘labels’
$labels = array(
‘name’ => ( ‘Things’ ),
‘singular_name’ => ( ‘Thing’ ),
‘add_new’ => ( ‘Add New’, ‘mythings’ ),
‘add_new_item’ => ( ‘Add New Thing’ ),
‘edit_item’ => ( ‘Edit Thing’ ),
‘new_item’ => ( ‘New Thing’ ),
‘view_item’ => ( ‘View Thing’ ),
‘search_items’ => ( ‘Search Things’ ),
‘not_found’ => ( ‘No thingamajigs found’ ),
‘not_found_in_trash’ => ( ‘No thingamajigs found in Trash’ ),
‘parent_item_colon’ => ”
);

// setup an array called $args to insert into ‘args’
$args = array(
‘labels’ => $labels, // adds your $labels array from above
‘public’ => true,
‘publicly_queryable’ => true,
‘show_ui’ => true,
‘query_var’ => true,
‘capability_type’ => ‘post’,
‘hierarchical’ => false,
// choose name used within permalink structure
‘rewrite’ => array( ‘slug’ => ‘things’ ), // changes name in permalink structure
‘menu_position’ => null, // search WordPress Codex for menu_position parameters
‘supports’ => array(
‘title’,
‘editor’, // adds the default text editor
‘author’,
‘thumbnail’, // adds Featured Image metabox**
‘comments’
)
);

// now register the post type
register_post_type( ‘mythings’, $args ); // adds your $args array from above
// flush the rewrite rules so you don’t have to visit Settings > Permalinks
flush_rewrite_rules();
} // end of mythings_init() function
[/code]

**NOTE: Including ‘thumbnail’ in the support parameters hasn’t always worked for me on it’s own. If it doesn’t, you can add this function after that last “}”

[code]
// enable Featured Image attachments
add_theme_support(‘post-thumbnails’);
[/code]

Mental Note: You can define & redefine $labels, or any of your custom variables, as often as you wish.

If all went well, Things should now appear in your admin menu, and you should be able to view All Things and Add New. If that checks out, you are ready for the next step.

Part II: Create Your Custom Taxonomies

Taxonomies are not required, but if you do want to add them, now is the time. You’ll see that we can use much of the same format from above.

[code]
// add action to create your custom taxonomies
add_action( ‘init’, ‘create_mythings_taxonomies’ );

// use your action
function create_mythings_taxonomies() {

// add a hierarchical taxonomy called Group (same as Post Categories)

// create the array for ‘labels’
$labels = array(
‘name’ => ( ‘Groups’ ),
‘singular_name’ => ( ‘Group’ ),
‘search_items’ => ( ‘Search Groups’ ),
‘all_items’ => ( ‘All Groups’ ),
‘parent_item’ => ( ‘Parent Group’ ),
‘parent_item_colon’ => ( ‘Parent Group:’ ),
‘edit_item’ => ( ‘Edit Group’ ),
‘update_item’ => ( ‘Update Group’ ),
‘add_new_item’ => ( ‘Add New Group’ ),
‘new_item_name’ => ( ‘New Group Name’ ),
);

// register your Groups taxonomy
register_taxonomy( ‘mt_groups’, array( ‘mythings’ ), array(
‘hierarchical’ => true,
‘labels’ => $labels, // adds the above $labels array
‘show_ui’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘mygroups’ ), // changes name in permalink structure
));

// add a non-hierarchical taxonomy called Flags (same as Post Tags)

// create the array for ‘labels’
$labels = array(
‘name’ => ( ‘Flags’ ),
‘singular_name’ => ( ‘Flag’ ),
‘search_items’ => ( ‘Search Flags’ ),
‘popular_items’ => ( ‘Popular Flags’ ),
‘all_items’ => ( ‘All Flags’ ),
‘parent_item’ => null,
‘parent_item_colon’ => null,
‘edit_item’ => ( ‘Edit Flag’ ),
‘update_item’ => ( ‘Update Flag’ ),
‘add_new_item’ => ( ‘Add New Flag’ ),
‘new_item_name’ => ( ‘New Flag Name’ ),
‘separate_items_with_commas’ => ( ‘Separate Flags with commas’ ),
‘add_or_remove_items’ => ( ‘Add or remove Flags’ ),
‘choose_from_most_used’ => ( ‘Choose from the most used Flags’ )
);

// register your Flags taxonomy
register_taxonomy( ‘mt_flags’, ‘mythings’, array(
‘hierarchical’ => false,
‘labels’ => $labels, // adds the above $labels array
‘show_ui’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘myflags’ ), // changes name in permalink structure
));

} // end of create_mythings_taxonomies() function
[/code]

Now your two custom taxonomies should show up in your My Things dropdown menu.

Part III: Create Your Meta Boxes

This is important if you want your custom post type to do something more than Posts & Pages, and I hope you do…

[code]
// add function to put metaboxes on the admin page
add_action( ‘admin_init’, ‘mt_admin_init’ );
// use the function
function mt_admin_init(){
add_meta_box(
‘mt_first_metabox’, // $id
‘First Metabox’, // $title
‘mt_first_metabox’, // $callback
‘mythings’, // $page /* stick with $post_type for now */
‘normal’, // $context /* ‘normal’ = main column. ‘side’ = sidebar */
‘high’ // $priority /* placement on admin page */
);
/* ADD ANOTHER META BOX HERE */

// design the first metabox
function mt_first_meta() {
global $post;
$custom = get_post_custom($post->ID);
$mt_field_one = $custom[‘mt_field_one’][0];
$mt_field_two = $custom[‘mt_field_two’][0];
// etcetera, etcetera
?> /* close the php tag to create your metabox layout with HTML */



Part IV: Save Your Data

Here is the best part — making it work.

[code]
// add your data-saving function
add_action(‘save_post’, ‘mt_save_data’);
// use your function
function mt_save_data(){
global $post;

update_post_meta($post->ID, ‘mt_field_one’, $_POST[‘mt_field_one’]);
update_post_meta($post->ID, ‘mt_field_two’, $_POST[‘mt_field_two’]);
/* INCLUDE FIELDS FROM ALL OF YOUR META BOXES HERE */
} // end mt_save_data() function
[/code]

Now test it out by filling in your fields on the admin page. Click save and watch your entries not disappear. If it works, you have earned your inflated sense of accomplishment.

There is plenty more functionality to play around with, but I must save that for my next post as this has gotten rather long.

Final: The Whole Shebang

[code]
// add your init() function
add_action( ‘init’, ‘mythings_init’ );
// use your init() function
function mythings_init() {
// setup an array called $labels to insert into ‘labels’
$labels = array(
‘name’ => ( ‘Things’ ),
‘singular_name’ => ( ‘Thing’ ),
‘add_new’ => ( ‘Add New’, ‘mythings’ ),
‘add_new_item’ => ( ‘Add New Thing’ ),
‘edit_item’ => ( ‘Edit Thing’ ),
‘new_item’ => ( ‘New Thing’ ),
‘view_item’ => ( ‘View Thing’ ),
‘search_items’ => ( ‘Search Things’ ),
‘not_found’ => ( ‘No thingamajigs found’ ),
‘not_found_in_trash’ => ( ‘No thingamajigs found in Trash’ ),
‘parent_item_colon’ => ”
);

// setup an array called $args to insert into ‘args’
$args = array(
‘labels’ => $labels, // adds your $labels array from above
‘public’ => true,
‘publicly_queryable’ => true,
‘show_ui’ => true,
‘query_var’ => true,
‘capability_type’ => ‘post’,
‘hierarchical’ => false,
// choose name used within permalink structure
‘rewrite’ => array( ‘slug’ => ‘things’ ), // changes name in permalink structure
‘menu_position’ => null, // search WordPress Codex for menu_position parameters
‘supports’ => array(
‘title’,
‘editor’, // adds the default text editor
‘author’,
‘thumbnail’, // adds Featured Image metabox**
‘comments’
)
);

// now register the post type
register_post_type( ‘mythings’, $args ); // adds your $args array from above
// flush the rewrite rules so you don’t have to visit Settings > Permalinks
flush_rewrite_rules();
} // end of mythings_init() function

// add action to create your custom taxonomies
add_action( ‘init’, ‘create_mythings_taxonomies’ );

// use your action
function create_mythings_taxonomies() {

// add a hierarchical taxonomy called Group (same as Post Categories)

// create the array for ‘labels’
$labels = array(
‘name’ => ( ‘Groups’ ),
‘singular_name’ => ( ‘Group’ ),
‘search_items’ => ( ‘Search Groups’ ),
‘all_items’ => ( ‘All Groups’ ),
‘parent_item’ => ( ‘Parent Group’ ),
‘parent_item_colon’ => ( ‘Parent Group:’ ),
‘edit_item’ => ( ‘Edit Group’ ),
‘update_item’ => ( ‘Update Group’ ),
‘add_new_item’ => ( ‘Add New Group’ ),
‘new_item_name’ => ( ‘New Group Name’ ),
);

// register your Groups taxonomy
register_taxonomy( ‘mt_groups’, array( ‘mythings’ ), array(
‘hierarchical’ => true,
‘labels’ => $labels, // adds the above $labels array
‘show_ui’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘mygroups’ ), // changes name in permalink structure
));

// add a non-hierarchical taxonomy called Flags (same as Post Tags)

// create the array for ‘labels’
$labels = array(
‘name’ => ( ‘Flags’ ),
‘singular_name’ => ( ‘Flag’ ),
‘search_items’ => ( ‘Search Flags’ ),
‘popular_items’ => ( ‘Popular Flags’ ),
‘all_items’ => ( ‘All Flags’ ),
‘parent_item’ => null,
‘parent_item_colon’ => null,
‘edit_item’ => ( ‘Edit Flag’ ),
‘update_item’ => ( ‘Update Flag’ ),
‘add_new_item’ => ( ‘Add New Flag’ ),
‘new_item_name’ => ( ‘New Flag Name’ ),
‘separate_items_with_commas’ => ( ‘Separate Flags with commas’ ),
‘add_or_remove_items’ => ( ‘Add or remove Flags’ ),
‘choose_from_most_used’ => ( ‘Choose from the most used Flags’ )
);

// register your Flags taxonomy
register_taxonomy( ‘mt_flags’, ‘mythings’, array(
‘hierarchical’ => false,
‘labels’ => $labels, // adds the above $labels array
‘show_ui’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘myflags’ ), // changes name in permalink structure
));

} // end of create_mythings_taxonomies() function

// add function to put metaboxes on the admin page
add_action( ‘admin_init’, ‘mt_admin_init’ );
// use the function
function mt_admin_init(){
add_meta_box(
‘mt_first_metabox’, // $id
‘First Metabox’, // $title
‘mt_first_metabox’, // $callback
‘mythings’, // $page /* stick with $post_type for now */
‘normal’, // $context /* ‘normal’ = main column. ‘side’ = sidebar */
‘high’ // $priority /* placement on admin page */
);
/* ADD ANOTHER META BOX HERE */

// design the first metabox
function mt_first_meta() {
global $post;
$custom = get_post_custom($post->ID);
$mt_field_one = $custom[‘mt_field_one’][0];
$mt_field_two = $custom[‘mt_field_two’][0];
// etcetera, etcetera
?> /* close the php tag to create your metabox layout with HTML */



ID, ‘mt_field_one’, $_POST[‘mt_field_one’]);
update_post_meta($post->ID, ‘mt_field_two’, $_POST[‘mt_field_two’]);
/* INCLUDE FIELDS FROM ALL OF YOUR META BOXES HERE */
} // end mt_save_data() function
[/code]

How to backup to Amazon S3

## UPDATE 2017 ##
The method described below was in use 5 years ago. It may still work on your host, but there are more elegant ways to do this now. I keep it here for archive purposes only.
##############

Need to backup your Linux server files and databases to Amazon S3? Here is a great tool – http://s3tools.org/download

Follow the install instructions.

Once you set up your S3 bucket, you can use this bash script to copy files to the S3 bucket:


#!/bin/sh
#
## Lanexa.net - file name = BACKUP_s3.sh
## backup script to push archive files to Amazon S3 bucket
## 2012-01-04
#
# define where your backup files are stored locally on your server
FILEDIR=/BACKUP/files/
DBDIR=/BACKUP/mysql/
#
cd $FILEDIR
#
# use s3cmd to upload files to S3 instance
s3cmd put FILENAME s3://BUCKET-NAME/
#
cd $DBDIR
s3cmd put DATABASES s3://BUCKET-NAME/
#
# email a note to yourself that backup is complete
mail -s "backup files and databases copied to S3" youremail@address.com

Create a WordPress Child-theme on Thematic framework

I’m a huge fan of ThemeShaper‘s Thematic base theme, by Ian Stewart. After spending some time getting to know it, I have very little reason to start my custom themes from scratch. Since coming to understand the structure, I have yet to feel limited by it. View a diagram of the Thematic structure here. After a few projects, you’ll learn to see that plain white wireframe as an endless ocean of all possibility.

Obviously, start by installing the latest version of Thematic. Then decide whether you need a simple standard child theme, or a highly customized child theme.

Standard Sub-theme: The developers kindly offer a convenient way to implement a standard child-theme. From your FTP client:

  • Navigate to wordpress/wp-content/themes/thematic
  • Duplicate the directory thematicsamplechildtheme, copy it into the themes directory, and rename it to your liking.
  • Go inside your child-theme directory, open the style.css, and edit the info found in the head of the document accordingly.

At this point, you should be able to activate your child-theme from the WordPress backend. Have at it!

Custom Sub-theme: For complete control over the design, you will want to override many Thematic files. To do so:

  • Duplicate the thematic directory and rename it accordingly
  • Open your new style.css, and delete the contents.
  • Replace the contents with something like this:

[code]
/*
Theme Name: Custom Child Theme
Theme URI:
Description: A Child Theme on Thematic framework.
Author: Your Name
Author URI: http://yourdomain.com/
Template: thematic
Version: 1.0
Tags: Thematic
.
Thematic is © Ian Stewart http://themeshaper.com/
.
*/

@import url(‘../thematic/style.css’);

/* Your Child Theme CSS Here */
[/code]
*Note: The document head is the same as in their standard child theme style.css.
*Note: We are importing the main stylesheet from the parent theme.

  • Open your new functions.php, and delete the contents.
  • Optionally, you can replace the contents with something like this:

[code]
<?php

//
// Custom Child Theme Functions
//

// I’ve included a “commented out” sample function below that’ll add a home link to your menu
// More ideas can be found on “A Guide To Customizing The Thematic Theme Framework”
// http://themeshaper.com/thematic-for-wordpress/guide-customizing-thematic-theme-framework/

// Adds a home link to your menu
// http://codex.wordpress.org/Template_Tags/wp_page_menu
//function childtheme_menu_args($args) {
// $args = array(
// ‘show_home’ => ‘Home’,
// ‘sort_column’ => ‘menu_order’,
// ‘menu_class’ => ‘menu’,
// ‘echo’ => true
// );
// return $args;
//}
//add_filter(‘wp_page_menu_args’,’childtheme_menu_args’);

// Unleash the power of Thematic’s dynamic classes
//
// define(‘THEMATIC_COMPATIBLE_BODY_CLASS’, true);
// define(‘THEMATIC_COMPATIBLE_POST_CLASS’, true);

// Unleash the power of Thematic’s comment form
//
// define(‘THEMATIC_COMPATIBLE_COMMENT_FORM’, true);

// Unleash the power of Thematic’s feed link functions
//
// define(‘THEMATIC_COMPATIBLE_FEEDLINKS’, true);

?>
[/code]
*Note: This this the content offered in functions.php of the Thematic sample child theme.

Now you can also delete the library directory. You can build your own eventually, but this particular library must stay unique to the parent theme.

By copying over the Thematic files, you now have a duplicate theme overriding the parent, allowing you to make all the changes you want without harming the original.

Lastly, Thematic’s developers recommend copying their default.css into your style.css if you are going to be making major changes.

  • From your FTP client, navigate to themes/thematic/library/styles/.
  • Open default.css, and copy the contents.
  • Go back to edit your style.css, and paste in the copied contents at the bottom.

And there you have it — a major head start on a highly customizable theme.