1

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 ),
    )
  );
}
0

1 Answer 1

2

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.

1
  • +1 for the side note, i have seen so many users just using this without text domain
    – bravokeyl
    Apr 26, 2016 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.