Skip to content

Instantly share code, notes, and snippets.

@Neshable
Created February 26, 2017 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Neshable/9b6209ce55f064aa17f43c839c58911a to your computer and use it in GitHub Desktop.
Save Neshable/9b6209ce55f064aa17f43c839c58911a to your computer and use it in GitHub Desktop.
Adding Custom Post Type to WordPress
// Register Custom Post Type
function books_post() {
// Array of custom labels for our custom post type backend.
$labels = array(
'name' => 'Books', //general name for the post type, usually plural. Default is Posts/Pages
'singular_name' => 'Book', //name for single object of this post type. Default is Post/Page
'menu_name' => 'Books', // Name used in the menu
'name_admin_bar' => 'Book', // String for use in New in Admin menu bar. - New Book
'add_new_item' => 'Add New Book', // Default is Add New Post/Add New Page.
'new_item' => 'New Book', // Default is New Post/New Page.
'edit_item' => 'Edit Book', // Default is Edit Post/Edit Page.
'view_item' => 'View Book', // Default is View Post/View Page.
'all_items' => 'All Books', // String for the submenu. Default is All Posts/All Pages.
'search_items' => 'Search Books', // Default is Search Posts/Pages
'parent_item_colon' => 'Parent Books:', // This string is used in hierarchical types. The default is 'Parent Page:'.
'not_found' => 'No books found.', // Default is No posts found/No pages found.
'not_found_in_trash' => 'No books found in Trash.', //Default is No posts found in Trash/No pages found in Trash.
'featured_image' => 'Book Cover', // Default is Featured Image.
'set_featured_image' => 'Set book cover', // Default is Set featured image.
'remove_featured_image' => 'Remove book cover', // Default is Remove featured image.
'use_featured_image' => 'Use as book cover', // Default is Use as featured image.
'insert_into_item' => 'Insert into item', // String for the media frame button. Default is Insert into post/Insert into page.
'uploaded_to_this_item' => 'Uploaded to this item', // - String for the media frame filter. Default is Uploaded to this post/Uploaded to this page.
'items_list' => 'Books list', // String for the table hidden heading.
'items_list_navigation' => 'Books list navigation', // String for the table pagination hidden heading.
'filter_items_list' => 'Filter items list' // String for the table views hidden heading.
);
$args = array(
// A plural descriptive name for the post type marked for translation. If you don’t declare a custom label, WordPress will use the name of the custom post type by default.
'label' => 'Book',
// Applying our labels array from above.
'labels' => $labels,
// This is usually an array of features that the custom post type will support. Here we have quite a long list. These will tie into the admin area.
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'comments', 'custom-fields', ),
// An array of registered taxonomies like category or post_tag that will be used with this post type. Custom taxonomies still need to be registered with register_taxonomy().
'taxonomies' => array( 'category', 'post_tag' ),
// Whether the post type is hierarchical (e.g. page). Or whether or not you can declare a parent page, child page, etc… of the post type. This is mainly intended for Pages. Here we declare it false so there’s no need to worry about it for our example.
'hierarchical' => false,
// Whether a post type is intended to be used publicly either via the admin interface or by front-end users. WordPress sets this to false by default.
'public' => true,
// Generates a default UI for managing this post type in the admin.
'show_ui' => true,
// Whether the custom post type should be visible in the menu.
'show_in_menu' => true,
// The position in the menu order the post type should appear;5 - below Posts; 10 - below Media; 15 - below Links; 20 - below Pages; 25 - below comments; 60 - below first separator; 65 - below Plugins; 70 - below Users; 75 - below Tools; 80 - below Settings; 100 - below second separator.
'menu_position' => 20,
// This declares a custom icon for the admin area. For more dashicons see the original WordPress documentation.
'menu_icon' => 'dashicons-format-aside',
// Whether to make this post type available in the WordPress admin bar.
'show_in_admin_bar' => true,
//Whether post_type is available for selection in navigation menus.
'show_in_nav_menus' => true,
// Enables post type archives.
'has_archive' => true,
// Exclude for search engine
'exclude_from_search' => false,
// Whether queries can be performed on the front end as part of parse_request()
'publicly_queryable' => true,
// Here we can declare what type of custom post type we will be dealing with. It is used to build the read, edit, and delete capabilities of a post or page. You can choose either post or page.
'capability_type' => 'page',
// Whether to expose this post type in the REST API.
'show_in_rest' => true
);
// The register_post_type() is a function that WordPress recognizes as a custom post type generator. In this example it accepts two parameters which are the name of the post type itself and any arguments you would like to call.
register_post_type( 'books', $args );
}
// This line of code returns or calls our function so it fires and displays within our site.
add_action( 'init', 'books_post' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment