How to Set a Default WP Editor for a Custom Post Type
WordPress lets you switch between the Classic and Gutenberg editor, but this is a site-wide change. What if you want your regular posts to be in the Classic editor, and your custom posts in Gutenberg? Or vice-versa?
I wrote this functions.php snippet to come to the rescue.
A requirement for this snippet to work is that you install the Classic Editor WordPress plugin. Chances are good that you already have this plugin, as it's in the top 5 most downloaded plugins of all time.
Once you're ensured that plugin is installed, simply add this to your functions.php, taking care to swap out your_custom_post_type with the name of your custom post type.
/*
* Set a default editor for a custom post type
*/
add_action('admin_menu', 'modify_new_post_link_for_custom_post_type', 999);
function modify_new_post_link_for_custom_post_type() {
global $submenu;
$post_type = 'your_custom_post_type'; // Change this to your custom post type
$set_gutenberg = true; // true = Gutenberg, false = Classic editor
if (isset($submenu['edit.php?post_type=' . $post_type])) {
foreach ($submenu['edit.php?post_type=' . $post_type] as &$item) {
if ($item[2] === 'post-new.php?post_type=' . $post_type) {
if ($set_gutenberg) {
$item[2] .= '&classic-editor__forget';
} else {
$item[2] .= '&classic-editor__forget&classic-editor';
}
}
}
}
}
For the $set_gutenberg section, this is set to "true" by default, which means it will load Gutenberg when you go to add a new custom post. If you want to turn off Gutenberg for your custom post type, set this to false.
What if I want to add this to more than one custom post type?
It's pretty easy to adapt this! You can tweak this line to add two custom post types:
$post_type = ['your_custom_post_type', 'another_post_type'];
Cool, right? Drop me a comment if this helped you!
Comments