Better Practice, Dec. 2018

(singke) #1
6 \ December 2018 \ http://www.phparch.com

Custom Post Types in WordPress

register_post_type( 'wiverb_bike',
// (condensed; see full example above)
'rewrite' => array( 'slug' => 'trails/hiking' ),
'has_archive' => 'trails/hiking'
);

Before: oursite.com/hiking.
After: oursite.com/trails/hiking.

Important: Remember to flush and rebuild your perma-
links after you make this change to your custom post type.

This step is one of those lessons I’ve learned the hard way
when I’ve changed permalinks then wonder why my new
links lead to 404s.
You can flush and rebuild permalinks manually by going
into WP Admin Settings > Permalinks and choosing Plain,
then click on save. Then go back to the “Post name” or
“Custom Structure” you had set up before and save again.


You can also code a rewrite rule to perform a permalinks
flush to your theme or plugin. A flush is an expensive oper-
ation; it’s best to do this task only when needed, for example
during a plugin activation or a theme switch. For more infor-
mation, see the WordPress Documentation on flush rewrite
rules^6.

Creating Your Plugin for Custom Post Types
What if you plan to change themes? If all of your code
is tied up in functions.php, you have to move it to the new
theme’s functions.php file. Luckily, your data will persist in
the WordPress database, but you would need to rebuild the
ability to access this content.
One solution to this is to use a third party plugin to build
your custom post types or, better yet, create your plugin.
If you intend to create a plugin for distribution to the
community, WordPress has standards you should follow.
When I’m building a plugin for use on my sites, I like to keep
them pretty simple.
For example, the simple plugin created by Listing 5 contains
much of the code above and leverages the WordPress init
hook to load support for our custom post types in the WP
Admin.

Be sure to remove your custom post type code from your
theme’s functions.php file before you activate your plugin
containing the same functions to prevent errors.

Adding Data to Your Custom Post Types
Now that you’ve created your custom post type, it’s time
to add some data. If you chose to include title, editor, and
thumbnail in your supports declaration when you registered

6 flush rewrite rules: https://phpa.me/wp-flush-rewrite-rules

your custom post type, the editing experience might look just
like your Pages’ editing experience.
This way, your site administrators can build out content
using the tools they are accustomed to using.
What About the Gutenberg Editor?
If you wish to enable the Gutenberg editor^7 for your custom
post type, you must first have the editor enabled through
the supports array. You must also set show_in_rest to true
because the Gutenberg editor was built using JavaScript and
the WordPress REST API.

register_post_type( 'mycpt_bike',
// (condensed; see full example above)
'supports' => array('title', 'editor', 'thumbnail'),
'show_in_rest' => true
);

It’s also possible to disable the Gutenberg editor for your
custom post type. Some of the sites I manage leverage
custom fields for structured data, have custom shortcodes,
or TinyMCE editor customizations, so I plan to disable the
Gutenberg editor while I learn how to better integrate it with
these sites.
If you wish to disable Gutenberg for the entire site, you can
do so with a filter. For Gutenberg 4.1 and newer use:

add_filter('use_block_editor_for_post', '__return_false');

For older versions you should use:

add_filter('gutenberg_can_edit_post_type', '__return_false');

7 Gutenberg editor: https://wordpress.org/gutenberg/

Figure 3. Nested Menu
Free download pdf