Toolsnip

Wordpress: Custom Post Type Registration

Learn how to register a custom post type in WordPress with this code snippet, which includes full support for the block editor, custom labels, and permissions.

Registering a custom post type in WordPress allows developers to add specialized content beyond posts and pages. This snippet provides a robust way to register a custom post type with support for archives, featured images, and custom taxonomies.

The function register_post_type is used, ensuring the custom post type is not only declared but also configured with labels for a better admin interface experience. This includes setting names, menu names, and other relevant properties.

Capabilities and permissions can be managed through the 'capabilities' parameter, making it possible to tailor access according to user roles. This is crucial for larger sites with multiple users having different roles.

To enhance SEO and ensure the content is correctly indexed, the 'rewrite' parameter is used to define a custom slug. This also improves the aesthetic and usability of the URLs.

Lastly, this snippet ensures compatibility with the block editor by supporting 'show_in_rest'. This allows the custom post type to fully utilize the Gutenberg editor, enhancing the content management experience.

Snippet Code

Version Compatibility

This code is compatible with WordPress 5.0 and later. It fully supports the block editor introduced in version 5.0.

Required PHP Version

PHP 7.2 or higher

Wordpress Core Functions Used

Use Cases

  • theme development
  • plugin development
  • custom content