Adding `supports` array to WordPress custom post type

I am trying to add the supports parameter to my custom post type but it is not working and renders the editing page blank:

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'my_campaigns',
    array(
      'labels' = array(
        'name' = __( 'Campaigns' ),
        'singular_name' = __( 'Campaign' )
      ),
      'supports' = array(
         'title' = true,
         'editor' = true,
         'thumbnail' = true ),
    )
  );
}

Topic post-type-support custom-post-types Wordpress

Category Web


Use 'supports' => [ 'title', 'editor', 'thumbnail' ] – different values, not keys.

If we look into register_post_type(), we find these lines:

if ( ! empty( $args->supports ) ) {
    add_post_type_support( $post_type, $args->supports );
    unset( $args->supports );
} elseif ( false !== $args->supports ) {
    // Add default features
    add_post_type_support( $post_type, array( 'title', 'editor' ) );
}

In add_post_type_support(), we see that the the values are converted to array keys with the value true:

function add_post_type_support( $post_type, $feature ) {
    global $_wp_post_type_features;

    $features = (array) $feature;
    foreach ($features as $feature) {
        if ( func_num_args() == 2 )
            $_wp_post_type_features[$post_type][$feature] = true;
        else
            $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 );
    }
}

register_post_type() is calling add_post_type_support() with just two arguments, so the else in the latter function cannot be reached.

You can however, call add_post_type_support() directly and pass custom support features:

add_post_type_support(
    'my_campaigns',
    'subheadline',
    [
        'min_length' => 30,
        'max_length' => 300,
    ]
);

But then you have to call the function for each feature separately.

Side note: Please do not use 'name' => __( 'Campaigns' ). Without a text domain, this string is not translatable at all, but it triggers a look-up in WordPress default translation, which is rather huge – and slow. See How to Internationalize Your Plugin for details.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.