LoginSignup
3
3

More than 5 years have passed since last update.

WordPress5.0 カスタム投稿でのGutenberg使用方法

Last updated at Posted at 2018-12-10

WordPress5.0のカスタム投稿

今回もWordPress5.0のメモ書きです。
様々な案件でカスタム投稿機能を使われる方が多いかと思うので誰かの参考になれば幸いです。

詳しくはこちらのIsuuを見てください。

要約するとカスタム投稿機能を使うと強制的にクラシックエディターに戻ってしまいます。
※案件によっては助かる場合も・・・・

そこでIsuuに上げられているのを発見したので、今回はその適用方法です。

register_post_type()に設定を加える

カスタム投稿機能を使用可能にする関数にパラメーターを1つ加えることにより、Gutenbergが適用されます。

'show_in_rest' => true

この「show_in_rest」パラメーターをtrueにすることでカスタム投稿でもGutenbergが反映されるようになるみたいです。
また、その際に「supports」パラメーター内に「editor」が設定されていることが必須のようです。
※「title」が無くてもGutenbergが適用されます。

このパラメーターはWordPress REST APIを使用してカスタム投稿の記事を取得したい場合に使用します。
JavaScriptで作られているGutenbergもREST APIを通してデータを取得しているようなので、
このパラメーターがTrueである事が使用条件になっているのでしょう。

全体的には下のようなコードになります。

register_post_type(
        'customPost',
        array(
            'labels' => array('ラベルのなんやかんや'),
            'menu_position' => 5,
            'public' => true,
            'has_archive' => true,
            'rewrite' => array(
                'slug' => 'customPost',
                'with_front' => false
            ),
            'show_in_rest' => true, // Gutenberg使用時に必要
            'supports' => array(
                'title',
                'editor', // Gutenberg使用時に必須
                'revisions'
            ),
            'map_meta_cap' => true
        )
    );
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3