We often need to register custom post type in WordPress to meet niche content publishing system. Post type can be very useful for SEO as it enables a complete series of url options with a key slug. In this case the key slug will the post type. Apart from that it can be very helpful to have a organized website which has different type or genre of contents.
In this below code snippets we will be able to add a custom post type named ‘code’ .
We need to tell the WordPress to add our custom post type function as it initialize the request. For this we need to ‘add_action’ in ‘int’ function of WordPress.
Like this:
Custom Post type Initialization:
add_action( 'init', 'function_name', 0 );
Now we need to construct some data as array to pass with the main WordPress register custom post type function ‘register_post_type’. This functions takes a lot of arguments to set the post type more accurately we need. But here we are looking at most common arguments of the function. You can check ‘register_post_type‘ this to know about all the arguments.
function code_post_type() { // this labels array contains all the labels/names required for wordpress to function a custom post type $labels = array( //This is the general post type name. 'name' => _x("Code Snippets", "post type general name"), //This is the singular version of that general names. WordPress shows it when it is handling single post item of this post type 'singular_name' => _x("Code Snippet", "post type singular name"), //Appears at places like WordPress dashboard menu item 'menu_name' => 'Code Snippets', //Appears at add new post button inside this post type 'add_new' => _x("Add New Snippet", "code item"), //Appears at add new item post button inside this post type 'add_new_item' => __("Add New Snippet"), //Appears at edit item post button inside this post type 'edit_item' => __("Edit Snippet"), //Appears at new item post button inside this post type 'new_item' => __("New Snippet"), //Appears at view item post button inside this post type 'view_item' => __("View Snippet"), //Appears at search button inside this post type 'search_items' => __("Search Snippets"), //Appears search result inside this post type 'not_found' => __("No Snippets Found"), //Appears search result in trash inside this post type 'not_found_in_trash' => __("No Snippets Found in Trash"), ); //we are creating array of argument or options that we need to register a custom post type. are simply calling it as $arg //see below explanation of the argument we are setting here. $arg = array( 'labels' => $labels, 'public' => true, 'has_archive' => true, 'menu_icon' => 'dashicons-media-code', 'rewrite' => array('slug' => 'code'), 'supports' => array('title', 'editor'), 'menu_position' => 5 ); // Calling the main Register post type with the first argument you create the slug of the post type register_post_type('code' , $arg); } add_action( 'init', 'code_post_type', 0 );
Important arguments:
labels: It store all the names can be used for operating a custom post type. Please read comment above each line in label section.
public: Takes bool value ‘true’ or ‘false’. If it’s set to ”true” then the post type content is intended to use publicly. If false then it will be intended to use by code or program or admins.
has_archive: Takes bool Value. sets whether the post type content should be archived or not. Default is false
menu_icon: Takes string value. You can use dashing class or a full URL for this. The icon will be shown beside the menu title of this post type.
rewrite: Means URL rewrite. Takes an array. We can simply make it array(‘slug’ => ‘code’) to set the post type slug to be ‘code’
supports: Takes an array with information for the editing support of the post from backed. array(‘title’, ‘editor’) means we ill need the title field and the editor. where ? we are writing the post currently the word press edit post page for this code custom post type.
menu_position: Takes integer value to set the position of the menu of this particular post item. The lower you put the menu item will be upper of the left menu of WordPress dashboard.
Thanks for reading. We have covered most common arguments of the register post type function. Feel free to check out the codex if you want to create more advanced and optional post type for your website. See the codex also how register_post_typo is writen by WordPress developers. Share your thoughts or code via comment.