WordPress add_post_type_support(): Add or Remove Features from Any CPT at Runtime

add_post_type_support(): add or remove features of any post type

What a post type offers in the editor (title, editor, excerpt, featured image, comments, revisions, custom fields) is defined by its “supports”. For your own post types you set them in register_post_type(); for post types of core and plugins, add_post_type_support() and remove_post_type_support() change them later. Supports are also free-form flags: a plugin can define its own feature and let other post types opt in.

Problem: Pages should not have comments, revisions should be enabled for a plugin’s post type that has them off, and a custom “gallery” box should appear only on post types that declare support for it.

Solution: Call add_post_type_support() and remove_post_type_support() on init after the post types are registered, check with post_type_supports(), and get all post types with a feature with get_post_types_by_support().

Changing features

<?php
add_action( 'init', function () {
    // No comments and trackbacks on pages
    remove_post_type_support( 'page', 'comments' );
    remove_post_type_support( 'page', 'trackbacks' );

    // Revisions for a plugin's post type
    add_post_type_support( 'event', 'revisions' );

    // Excerpts for pages, e.g. for search results and SEO descriptions
    add_post_type_support( 'page', 'excerpt' );
}, 20 ); // after plugins registered their post types on init

The core features are title, editor, author, thumbnail, excerpt, trackbacks, custom-fields, comments, revisions, page-attributes and post-formats. Removing comments hides the discussion box; existing comments stay in the database and the comment form depends on the theme, so also close comments if they should really stop.

A thumbnail also needs the theme’s add_theme_support( 'post-thumbnails' ); the post type support only adds the box.

Your own feature flags

<?php
add_action( 'init', function () {
    add_post_type_support( 'post', 'ha-gallery' );
    add_post_type_support( 'portfolio', 'ha-gallery' );
}, 20 );

add_action( 'add_meta_boxes', function () {
    foreach ( get_post_types_by_support( 'ha-gallery' ) as $post_type ) {
        add_meta_box( 'ha-gallery', __( 'Gallery', 'textdomain' ), 'ha_render_gallery_box', $post_type, 'normal' );
    }
} );

function ha_render_gallery_box( $post ) {
    // the gallery field...
}

A theme or another plugin can now turn the gallery on for its own post type with one line, without touching your code. post_type_supports( get_post_type(), 'ha-gallery' ) checks it in templates.

Arguments with a feature

A feature can carry extra settings, passed as the third argument and read back with get_all_post_type_supports():

<?php
add_post_type_support( 'portfolio', 'ha-gallery', array( 'max_images' => 12 ) );

$supports = get_all_post_type_supports( 'portfolio' );
$max      = isset( $supports['ha-gallery'][0]['max_images'] ) ? $supports['ha-gallery'][0]['max_images'] : 20;

NOTE: For WooCommerce products, check the product data tabs before changing supports: WooCommerce uses the excerpt as the product’s short description and the editor as its long description, and removing those supports removes the fields the shop templates display.