Post

Enable Gutenberg for Custom Post Type

If you want to use Gutenberg for editing your custom post types. And why wouldn’t you it’s amazing and easily the best option for WordPress content editing. Then you need to edit and add this piece of code into your functions.php to enable a Gutenberg. In this example we are initiating a custom post type called events. You’ll want to edit events to suit your custom post type.

Prerequisites

  • An active WordPress install, either local or on a remote server
  • The ability to edit the functions.php

The below cpt snippet utilises the 'show_in_rest' => true and 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ), which is are the key parts to enabling Gutenberg in custom post types.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function pxs_events_init() {
    $labels = array(
        'name'                  => _x( 'events', 'Post type general name', 'textdomain' ),
        'singular_name'         => _x( 'events', 'Post type singular name', 'textdomain' ),
        'menu_name'             => _x( 'events', 'Admin Menu text', 'textdomain' ),
        'name_admin_bar'        => _x( 'events', 'Add New on Toolbar', 'textdomain' ),
        'add_new'               => __( 'Add New', 'textdomain' ),
        'add_new_item'          => __( 'Add New events', 'textdomain' ),
        'new_item'              => __( 'New events', 'textdomain' ),
        'edit_item'             => __( 'Edit events', 'textdomain' ),
        'view_item'             => __( 'View events', 'textdomain' ),
        'all_items'             => __( 'All events', 'textdomain' ),
        'search_items'          => __( 'Search events', 'textdomain' ),
        'parent_item_colon'     => __( 'Parent events:', 'textdomain' ),
        'not_found'             => __( 'No s found.', 'textdomain' ),
        'not_found_in_trash'    => __( 'No s found in Trash.', 'textdomain' ),
        'featured_image'        => _x( 'events Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'set_featured_image'    => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'use_featured_image'    => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'archives'              => _x( 'events archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
        'insert_into_item'      => _x( 'Insert into ', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
        'uploaded_to_this_item' => _x( 'Uploaded to this ', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
        'filter_items_list'     => _x( 'Filter s list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
        'items_list_navigation' => _x( 'events list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
        'items_list'            => _x( 'events list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'menu_icon' => 'dashicons-tide',
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => '' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'show_in_rest' => true,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    );

    register_post_type( 'events', $args );
}

add_action( 'init', 'pxs_events_init' );
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.