[wp-trac] [WordPress Trac] #64156: add_post_type_support() does not merge sub-properties for the same feature
WordPress Trac
noreply at wordpress.org
Tue Oct 28 07:20:07 UTC 2025
#64156: add_post_type_support() does not merge sub-properties for the same feature
--------------------------------------+------------------------------
Reporter: fabiankaegy | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version:
Severity: normal | Resolution:
Keywords: has-patch has-unit-tests | Focuses:
--------------------------------------+------------------------------
Description changed by fabiankaegy:
Old description:
> = Summary =
>
> When `add_post_type_support()` is called multiple times for the same post
> type and feature with associative array arguments, the second call
> overwrites the first call's arguments instead of merging them. This
> prevents incremental addition of sub-properties to post type features.
>
> = Current Behavior =
>
> {{{
> add_post_type_support( 'page', 'editor', array(
> 'default-mode' => 'template-locked',
> ) );
>
> add_post_type_support( 'page', 'editor', array(
> 'block-comments' => true,
> ) );
>
> // Result: Only 'block-comments' is stored, 'default-mode' is lost
> var_dump( get_all_post_type_supports( 'page' )['editor'] );
> // array( array( 'block-comments' => true ) )
> }}}
>
> = Expected Behavior =
>
> Multiple calls should merge the array arguments, allowing different
> plugins/themes to add their own sub-properties without conflicting:
>
> {{{
> add_post_type_support( 'page', 'editor', array(
> 'default-mode' => 'template-locked',
> ) );
>
> add_post_type_support( 'page', 'editor', array(
> 'block-comments' => true,
> ) );
>
> // Expected: Both properties should be preserved
> var_dump( get_all_post_type_supports( 'page' )['editor'] );
> // array( array( 'default-mode' => 'template-locked', 'block-comments' =>
> true ) )
> }}}
>
> = Technical Details =
>
> The issue is in `/src/wp-includes/post.php` at line 2292 (as of [xxxxx]),
> where the function simply overwrites the existing value:
>
> {{{
> if ( $args ) {
> $_wp_post_type_features[ $post_type ][ $feature ] = $args;
> }
> }}}
>
> = Proposed Solution =
>
> Detect when both the existing and new values are associative arrays and
> merge them using `array_merge()`. This maintains backwards compatibility
> because:
>
> - If the feature doesn't exist yet, it's created as before
> - If the feature is set to `true` (no args), calling with args still
> overwrites it as expected
> - Only when both values are arrays does merging occur
> - Non-associative array arguments continue to work as before
>
> = Use Cases =
>
> This is particularly useful for:
>
> 1. **Block editor settings** - Different plugins adding different editor
> configuration options
> 2. **Custom feature flags** - Themes and plugins progressively enhancing
> the same feature
> 3. **Multi-plugin compatibility** - Avoiding conflicts when multiple
> plugins extend the same post type feature
>
> = Impact =
>
> This change would improve the API's usability and prevent unexpected
> behavior when multiple plugins/themes interact with the same post type
> features. The fix maintains full backwards compatibility.
New description:
= Summary =
When `add_post_type_support()` is called multiple times for the same post
type and feature with associative array arguments, the second call
overwrites the first call's arguments instead of merging them. This
prevents incremental addition of sub-properties to post type features.
= Current Behavior =
{{{
add_post_type_support( 'page', 'editor', array(
'default-mode' => 'template-locked',
) );
add_post_type_support( 'page', 'editor', array(
'block-comments' => true,
) );
// Result: Only 'block-comments' is stored, 'default-mode' is lost
var_dump( get_all_post_type_supports( 'page' )['editor'] );
// array( array( 'block-comments' => true ) )
}}}
= Expected Behavior =
Multiple calls should merge the array arguments, allowing different
plugins/themes to add their own sub-properties without conflicting:
{{{
add_post_type_support( 'page', 'editor', array(
'default-mode' => 'template-locked',
) );
add_post_type_support( 'page', 'editor', array(
'block-comments' => true,
) );
// Expected: Both properties should be preserved
var_dump( get_all_post_type_supports( 'page' )['editor'] );
// array( array( 'default-mode' => 'template-locked', 'block-comments' =>
true ) )
}}}
= Technical Details =
The issue is in `/src/wp-includes/post.php` at line 2292, where the
function simply overwrites the existing value:
{{{
if ( $args ) {
$_wp_post_type_features[ $post_type ][ $feature ] = $args;
}
}}}
= Proposed Solution =
Detect when both the existing and new values are associative arrays and
merge them using `array_merge()`. This maintains backwards compatibility
because:
- If the feature doesn't exist yet, it's created as before
- If the feature is set to `true` (no args), calling with args still
overwrites it as expected
- Only when both values are arrays does merging occur
- Non-associative array arguments continue to work as before
= Use Cases =
This is particularly useful for:
1. **Block editor settings** - Different plugins adding different editor
configuration options
2. **Custom feature flags** - Themes and plugins progressively enhancing
the same feature
3. **Multi-plugin compatibility** - Avoiding conflicts when multiple
plugins extend the same post type feature
= Impact =
This change would improve the API's usability and prevent unexpected
behavior when multiple plugins/themes interact with the same post type
features. The fix maintains full backwards compatibility.
--
--
Ticket URL: <https://core.trac.wordpress.org/ticket/64156#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list