possible bug in register_post_type() in handling of 'supports' arg
-
I’ve discovered what I think is a bug in register_post_type(). I’ve searched the forums and tickets and haven’t found anything about this issue. I wanted to ask about it here before opening a ticket to make sure I’m correct.
Suppose I call:
register_post_type ('test', array ('supports' = array ('title', 'revisions'))) ; $supports = array_keys (get_all_post_type_supports ('test')) ; // $supports is now array ('title', 'revisions'), as it should be
After that, suppose I call:
// re-register post type with additional supported features register_post_type ('test', array ('supports' => array ('title', 'editor', 'revisions'))) ; $supports = array_keys (get_all_post_type_supports ('test')) ; // $supports is now array ('title', 'editor', 'revisions'), as it should be
After that, suppose I call:
// re-register post type with fewer supported features register_post_type ('test', array ('supports' => array ('title', 'editor'))) ; $supports = array_keys (get_all_post_type_supports ('test')) ; // $supports is now array ('title', 'editor', 'revisions'), but should be array ('title', 'editor')
That is, if I re-register a post type with more supported features, those extra features are there. However, if I re-register a post type with fewer supported features, the larger list of features is still there.
Digging into the core source for register_post_type() at line 1277, I see
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')); }
and at line 1578, I see:
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 ); } }
Since add_post_type_support() just (as it’s name implies) adds to whatever features are already there, shouldn’t register_post_type() remove any existing supported features before calling add_post_type_support()?
That is, shouldn’t there be something similar to the following added before line 1278?
global $_wp_post_type_features ; if (isset ($_wp_post_type_features[$post_type])) { $_wp_post_type_features[$post_type] = array () ; }
Note: I’m not suggesting this is the best fix (I’m not that familiar with the core code), but it seems to produce the correct behavior.
- The topic ‘possible bug in register_post_type() in handling of 'supports' arg’ is closed to new replies.