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]

Get More out of WordPress Link Categories

UPDATE: August 1, 2013

The WordPress Links Manager has since been depreciated. Any virgin installments newer than 3.0 won’t have it, but you can add the feature back with the Link Manager plugin.

ORIGINAL POST:

The WordPress platform comes with an intriguing – yet often neglected – link managing feature in the Dashboard menu called Links. What’s intriguing is the potential power of this feature, particularly with respect to Link Categories. I emphasize potential power because it’s stock usage is currently limited to a sidebar widget. As Lanexa.net has recently been tasked with redesigning a site that maintains a substantial amount of external links, this article will demonstrate an easy way to increase the functionality of the link managing feature – by creating a landing page for your Link Categories (a.k.a. Bookmarks), which can be accessed from your main menu.

Step-by-Step

1. Create Desired Categories. In the WordPress Dashboard, go to Links > Link Categories to create the Name & Slug of the the categories you will be using. It’s not important to complete this step right away, but you’ll want to get a few things in there so you’ll have something to display further on.

2. Decide which Page Template to Use. This example will use the theme’s default template called page.php. If you’re working on a live site and don’t want to risk screwing something up, you can copy this template file and rename it, i.e. page-bkmrks.php.

3. Create the Landing Page From the Dashboard, add a new page and title it what you will, i.e. Bookmarks Lading Page. Select the template you will be using and save the page. Make note of the permalink url, underneath the title, before leaving the page. I recommend using pretty permalinks, for added convenience (and a million other reasons).

4. Edit the Template. Good code-editing software – such as Coda – will allow you to log on to your server, locate the active theme folder, duplicate & rename the appropriate file (if necessary), and open it up for editing. All you have to do now is insert this simple hook into the content div:
[code]
<?php
if (is_page(‘bookmarks-landing-page’)) {
wp_list_bookmarks();
}
?>
[/code]
Note: Using pretty permalinks allows me to select the page by title.

5. Visit Your New Bookmarks Landing Page. You will notice that all your bookmarks are displayed on the page, organized under their category titles. If this is all you are looking to do, you are free to add this page to your main menu like you would any other. For more advanced options, read on (or search “wp_list_bookmarks“).

6. Select which Categories to Display. You can modify the above code in order to customize which link categories will be displayed on which page:
[code]
<?php
if (is_page(‘bookmarks-landing-page’)) {
wp_list_bookmarks(‘categorize=1&category=2,5’);
}
?>
[/code]
Note: categorize=1 will list the links beneath their corresponding category title, while categorize=0 – also categorize= (no value) – will list links under the general heading Bookmarks.
Note: category=2,5 calls the desired link categories by their ID numbers. A category’s ID number is revealed in the URL while editing it from the Dashboard.

Or, perhaps it makes more sense to exclude a category from the list:
[code]
<?php
if (is_page(‘bookmarks-landing-page’)) {
wp_list_bookmarks(‘categorize=1&exclude_category=3,4,6’);
}
?>
[/code]
7. Display Different Categories on Different Pages. Create as many pages as you want (using the same template) to display as many different category combinations as you want. All you need to do is stack these conditional statements, one after the other:
[code]
<?php
if (is_page(‘bookmarks-landing-page’)) {
wp_list_bookmarks();
}
?>

<?php
if (is_page(‘bookmarks-landing-page-2’)) {
wp_list_bookmarks(‘categorize=0&exclude_category=3’);
}
?>

<?php
if (is_page(‘bookmarks-landing-page-3’)) {
wp_list_bookmarks(‘categorize=1&category=3,4,8’);
}
?>
[/code]
Note: A complete list of parameters is available in the WordPress Codex.
We hope to develop this functionality into a Plugin in the coming months. Until then, i hope you find this solution to be an easy-to-manage alternative.

How many members joined in the past week

I originally posted on the Drupal Support Forum here.

Here is how I did it in Drupal 5.11

Hope this helps someone. I created a block, then used Panels to add this block to my Panel page. Don’t forget to add the PHP opening and closing tags.
[code]
$result = db_fetch_array(db_query(“select count(uid) from users where week(from_unixtime(created)) = week(date_sub(now(), interval 1 week))”));

print $result[‘count(uid)’];
[/code]