Register a custom post type in WordPress

Posted by Register new custom post type WordPress

Registering a custom post type comes in handy pretty often when I develop WordPress solutions for my clients. Usually it’s for some kind of content, where there needs to be an archive or similar. I’ve used it for making custom galleries or for an employee to easily register and update football players in their squad.

This little walkthrough is just as much for my own sake as for yours. I have this little quirk where I somehow always end up selecting the wrong link to the WP codex, where the following snippet is copied from. So might as well make my own little page about it, where I can copy it from for future purposes.

Getting started

Take the following snippet and insert it into your functions.php. I would always recommend you to make use of a child theme, so that you can recieve updates to your theme.

add_action( 'init', 'codex_book_init' );
/**
 * Register a book post type.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_post_type
 */
function codex_book_init() {
	$labels = array(
		'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
		'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
		'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
		'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
		'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
		'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
		'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
		'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
		'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
		'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
		'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
		'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
		'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
		'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
	);

	$args = array(
		'labels'             => $labels,
                'description'        => __( 'Description.', 'your-plugin-textdomain' ),
		'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array( 'slug' => 'book' ),
		'capability_type'    => 'post',
		'has_archive'        => true,
		'hierarchical'       => false,
		'menu_position'      => null,
		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
	);

	register_post_type( 'book', $args );
}

 

What do I need to change?

You would want to change the following in order to make the custom post type visible:

your-plugin-textdomain
This should be the text domain of your current theme. You can always find this by opening the style.css of your theme. It will in the top say Text Domain:

( 'slug' => 'book' )
Here you would want to change the book part. This is what is going to be in your url. Fx. yourwebsite.com/book/rich-dad-poor-dad/

codex_book_init()
This is the function you are creating, to make the custom post type available. I would probably call it something like players_init() if it had something to do with football players. Remember to change it both at the functions part and add_action in the beginning.

Book or Books under $labels
This is all the different places the name will appear in your WordPress dashboard. You can kind of see the purpose of the different labels. F.e. menu_name is what it will be displayed as in your dashboard menu on the left.

register_post_type( 'book', $args );
This needs to be the same as your ( 'slug' => 'book' )-part. It is the part telling your WordPress installation, that you are registering a new post type.

 

Remember to save your permalinks

Once you’ve added the snippet to your functions.php and see it’s working on your website, it is very important to go to Settings and then Permalinks. Here you just need to scroll to the bottom and press Save changes, to make sure that your new post type is working correctly. The new posts might not show and pop up with a error 404: page not found, if you don’t go in and re-save your permalinks

Leave a Reply

Your e-mail address will not be published. Required fields are marked *