Make WordPress Core


Ignore:
Timestamp:
02/03/2023 12:13:52 AM (2 years ago)
Author:
hellofromTonya
Message:

Widgets: Preserve classic sidebars when switching to a block theme.

When switching to a block theme, classic sidebars were orphaned and their widgets remapping to the 'wp_inactive_widgets' sidebar . This changeset preserves the sidebars and their widgets, providing a migration path to a block theme without losing the widgets.

Classic sidebars are now:

  • Stored in a new theme mod called 'wp_classic_sidebars';
  • Restored to the $wp_registered_sidebars global variable when the 'widgets_init' action fires (via a new internal function called _wp_block_theme_register_classic_sidebars());
  • And marked as 'inactive' when interacting with sidebars REST API endpoint.

References:

Follow-up to [50995], [6334].

Props mamaduka, audrasjb, hellofromTonya, ironprogrammer, jameskoster, joen, matveb, mukesh27, noisysocks, poena, youknowriad.
Fixes #57531.

File:
1 edited

Legend:

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

    r54541 r55200  
    21062106    }
    21072107}
     2108
     2109/**
     2110 * Registers the previous theme's sidebars for the block themes.
     2111 *
     2112 * @since 6.2.0
     2113 * @access private
     2114 *
     2115 * @global array $wp_registered_sidebars Registered sidebars.
     2116 */
     2117function _wp_block_theme_register_classic_sidebars() {
     2118    global $wp_registered_sidebars;
     2119
     2120    if ( ! wp_is_block_theme() ) {
     2121        return;
     2122    }
     2123
     2124    $classic_sidebars = get_theme_mod( 'wp_classic_sidebars' );
     2125    if ( empty( $classic_sidebars ) ) {
     2126        return;
     2127    }
     2128
     2129    // Don't use `register_sidebar` since it will enable the `widgets` support for a theme.
     2130    foreach ( $classic_sidebars as $sidebar ) {
     2131        $wp_registered_sidebars[ $sidebar['id'] ] = $sidebar;
     2132    }
     2133}
Note: See TracChangeset for help on using the changeset viewer.