First Impressions of Custom Post Type

One of the new very interesting things in WordPress 3.0 are individual post-types you can implement with little effort. Back then, you had to expand the database and write your own interface for it, now you just have to add a few lines of code - of course this is just the current state, which can be change until the final release.

After Justin had been playing with these types, we check out the possibilities of types for "Movies".

Simple Solution

function post_type_movies() {
	register_post_type( 'movies',
                array( 'label' => __('Movies'), 'public' => true, 'show_ui' => true ) );
	register_taxonomy_for_object_type('post_tag', 'movies');
}
add_action('init', 'post_type_movies');

default custom post type

More parameters for meta-boxes

Of course there are a number of parameters for this function and so the behavior and appearance of the corresponding edit page can be controlled quite easily, a small sample with additional meta boxes:

function post_type_movies() {
	register_post_type(
                     'movies', 
                     array('label' => __('Movies'), 
                             'public' => true, 
                             'show_ui' => true,
                             'supports' => array(
                                        'post-thumbnails',
                                        'excerpts',
                                        'trackbacks',
                                        'custom-fields',
                                        'comments',
                                        'revisions')
                                ) 
                      );
	register_taxonomy_for_object_type('post_tag', 'movies');
}
add_action('init', 'post_type_movies');

The default arguments

// Args prefixed with an underscore are reserved for internal use.
$defaults = array(
    'label' => false,
    'publicly_queryable' => null,
    'exclude_from_search' => null,
    '_builtin' => false,
    '_edit_link' => 'post.php?post=%d',
    'capability_type' => 'post',
    'hierarchical' => false,
    'public' => false,
    'rewrite' => true,
    'query_var' => true,
    'supports' => array(),
    'register_meta_box_cb' => null,
    'taxonomies' => array(),
    'show_ui' => null
);
  • label - A descriptive name for the post type marked for translation. Defaults to $post_type
  • public - Whether posts of this type should be shown in the admin UI. Defaults to false
  • exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public
  • publicly_queryable - Whether post_type queries can be performed from the front page. Defaults to whatever public is set as
  • show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public
  • inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none
  • capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post"
  • edit_cap - The capability that controls editing a particular object of this post type. Defaults to "edit_$capability_type" (edit_post)
  • edit_type_cap - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts)
  • edit_others_cap - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts)
  • edit_others_cap - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts)
  • read_cap - The capability that controls reading a particular object of this post type. Defaults to "read_$capability_type" (read_post)
  • delete_cap - The capability that controls deleting a particular object of this post type. Defaults to "delete_$capability_type" (delete_post)
  • hierarchical - Whether the post type is hierarchical. Defaults to false
  • supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none
  • register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback
  • taxonomies - An array of taxonomy identifiers that will be registered for the post type. Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type()

Including Custom Taxonomies

In the following example we include in our Post-Type a Taxonomy with two possibilities; own Tags and categories for Post-Type Movies, the classical tag, without hierarchy and one as category, tag with hierarchies.

function post_type_movies() {
	register_post_type(
                'movies', 
                array(
                        'label' => __('Movies'),
                        'public' => true,
                        'show_ui' => true,
                        'supports' => array(
                                     'post-thumbnails',
                                     'excerpts',
                                     'trackbacks',
                                     'custom-fields',
                                     'comments',
                                     'revisions')
                )
        );
	
	register_taxonomy( 'actor', 'movies', array( 'hierarchical' => true, 'label' => __('Actor') ) ); 
	
        register_taxonomy( 'production', 'movies',
		array(
                         'hierarchical' => false,
			 'label' => __('Production'),
			 'query_var' => 'production',
			 'rewrite' => array('slug' => 'production' )
		)
	);
}
add_action('init', 'post_type_movies');

Definitely a very interesting and useful feature which provides many possibilities to play around with.

WordPress 3.0 Custom Background Support

In WordPress 3.0, it will have, additional to the Custom Header Image support, also a Custom Background Support. A first snapshot is in the current trunk. To use the functionality, the theme needs in functions.php this line:

add_custom_background();

After that, there is in your backend under Appearance the menu Custom Background available.

WordPress 3.0 Custom Background Step 1

After uploading an image, you have the option to activate "Tile the background" and delete the background image.

WordPress 3.0 Custom Background Support Step 2

Here the result with the new Theme Twenty Ten.

Theme Twenty Ten with Custom Background Support

WordPress: Useful Default Configuration Settings Via Plugin

Everybody who installs WordPress quite often knows the problem: You always have to do the same adjustments, for example deleting the Hello World post. That cost time and money. Our friend Thomas Scholz alias toscho had a simple but genius idea, he puts all needed option settings in a Plugin. Just activate the Plugin, deactivate it and delete it. Done!

You can adjust and expand the options as you like. A good overview is the wp-admin/options.php.

<?php
/*
Plugin Name: Toscho's basic settings
Plugin URI: http://toscho.de/2010/wordpress-grundeinstellungen-per-plugin-setzen/
Description: Some useful default configuration settings. See 'wp-admin/options.php' for more options.
Version: 0.2
Author: Thomas Scholz
Author URI: http://toscho.de
*/

function set_toscho_defaults()
{
    $o = array(
        'avatar_default'            => 'blank',
        'avatar_rating'             => 'G',
        'category_base'             => '/thema',
        'comment_max_links'         => 0,
        'comments_per_page'         => 0,
        'date_format'               => 'd.m.Y',
        'default_ping_status'       => 'closed',
        'default_post_edit_rows'    => 30,
        'links_updated_date_format' => 'j. F Y, H:i',
        'permalink_structure'       => '/%year%/%postname%/',
        'rss_language'              => 'de',
        'timezone_string'           => 'Etc/GMT-1',
        'use_smilies'               => 0,
    );

    foreach ( $o as $k => $v )
    {
        update_option($k, $v);
    }

    // Delete dummy post and comment.
    wp_delete_post(1, TRUE);
    wp_delete_comment(1);

    return;
}
register_activation_hook(__FILE__, 'set_toscho_defaults');
?>

Here you can download the Plugin by toscho.

WordPress 3.0 Specific Author Templates

WordPress 3.0 Author TemplateIn WordPress 3.0 it will be possible to use specific author templates. The function get_author_template() has been expanded in wp-includes/theme.php.

Now you can create a author-michael.php or author-123.php, which will be called via the WordPress Template Hierarchy in connection to the author with user_nicename michael or ID 123. Basically the same funtionality as we known for categories.