Make WordPress Core

Changeset 64681


Ignore:
Timestamp:
10/22/2026 08:29:24 PM (4 hours ago)
Author:
SergeyBiryukov
Message:

Widgets: Avoid sidebar name collision in dynamic_sidebar().

This ensures that dynamic_sidebar() displays the correct sidebar when it has the same ID as the sanitized name of another sidebar registered earlier.

An exact sidebar ID match is now prioritized over a sanitized name match, which may be less accurate.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/12166.

Follow-up to r5474.

Props sainathpoojary, wildworks, alexvorn2, markoheijnen, dilip2615, mosescursor, im3dabasia1, SergeyBiryukov.
Fixes #22116.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/widgets.php

    r64163 r64681  
    703703        } else {
    704704                $index = sanitize_title( $index );
    705                 foreach ( (array) $wp_registered_sidebars as $key => $value ) {
    706                         if ( sanitize_title( $value['name'] ) === $index ) {
    707                                 $index = $key;
    708                                 break;
     705
     706                if ( empty( $wp_registered_sidebars[ $index ] ) ) {
     707                        foreach ( (array) $wp_registered_sidebars as $key => $value ) {
     708                                if ( sanitize_title( $value['name'] ) === $index ) {
     709                                        $index = $key;
     710                                        break;
     711                                }
    709712                        }
    710713                }
  • trunk/tests/phpunit/tests/widgets.php

    r64038 r64681  
    407407
    408408                $this->assertSame( $sidebar_id, $this->sidebar_index );
     409        }
     410
     411        /**
     412         * @group sidebar
     413         * @ticket 22116
     414         */
     415        public function test_dynamic_sidebar_id_takes_priority_over_sanitized_name() {
     416                // Register a sidebar with the name 'Widget Area 121' (sanitizes to 'widget-area-121').
     417                register_sidebar(
     418                        array(
     419                                'id'   => 'widget-area-127',
     420                                'name' => 'Widget Area 121',
     421                        )
     422                );
     423
     424                /*
     425                 * Register a second sidebar whose ID is 'widget-area-121' —
     426                 * the same as the sanitized name of the first sidebar.
     427                 */
     428                register_sidebar(
     429                        array(
     430                                'id'   => 'widget-area-121',
     431                                'name' => 'Widget Area 121',
     432                        )
     433                );
     434
     435                add_action( 'dynamic_sidebar_before', array( $this, 'retrieve_sidebar_id' ), 10, 2 );
     436
     437                /*
     438                 * Passing 'widget-area-121' as an exact ID should resolve to that sidebar directly,
     439                 * not to 'widget-area-127' whose sanitized name also equals 'widget-area-121'.
     440                 */
     441                dynamic_sidebar( 'widget-area-121' );
     442
     443                $this->assertSame( 'widget-area-121', $this->sidebar_index );
    409444        }
    410445
Note: See TracChangeset for help on using the changeset viewer.