WordPress.org

Make WordPress Core

Opened 11 months ago

Closed 11 months ago

Last modified 11 months ago

#40009 closed defect (bug) (duplicate)

register_sidebar() mixes up widgets

Reported by: edge22 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.2
Component: Widgets Keywords: reporter-feedback
Focuses: Cc:

Description

Kind of an interesting issue.

Let's say you have a set of widgets created:

add_action( 'widgets_init', 'samples_widgets_init' );
function sample_widgets_init() 
{
	// Set up our array of widgets	
	$widgets = array(
		'sidebar-1' => __( 'Right Sidebar' ),
		'sidebar-2' => __( 'Left Sidebar' ),
		'header' => __( 'Header' ),
		'footer-1' => __( 'Footer Widget 1' ),
		'footer-2' => __( 'Footer Widget 2' ),
		'footer-3' => __( 'Footer Widget 3' ),
		'footer-4' => __( 'Footer Widget 4' ),
		'footer-5' => __( 'Footer Widget 5' ),
		'footer-bar' => __( 'Footer Bar' )
	);
	
	// Loop through them to create our widget areas
	foreach ( $widgets as $id => $name ) {
		register_sidebar( array(
			'name'          => $name,
			'id'            => $id,
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h4 class="widget-title">',
			'after_title'   => '</h4>',
		) );
	}
}

Works perfectly.

Ok, now let's insert a new widget area into the mix (not at the end).

add_action( 'widgets_init', 'samples_widgets_init' );
function sample_widgets_init() 
{
	// Set up our array of widgets	
	$widgets = array(
		'sidebar-1' => __( 'Right Sidebar' ),
		'sidebar-2' => __( 'Left Sidebar' ),
		'top-bar' => __( 'Top Bar' ),
		'header' => __( 'Header' ),
		'footer-1' => __( 'Footer Widget 1' ),
		'footer-2' => __( 'Footer Widget 2' ),
		'footer-3' => __( 'Footer Widget 3' ),
		'footer-4' => __( 'Footer Widget 4' ),
		'footer-5' => __( 'Footer Widget 5' ),
		'footer-bar' => __( 'Footer Bar' )
	);
	
	// Loop through them to create our widget areas
	foreach ( $widgets as $id => $name ) {
		register_sidebar( array(
			'name'          => $name,
			'id'            => $id,
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h4 class="widget-title">',
			'after_title'   => '</h4>',
		) );
	}
}

So we've added a top bar widget area above the header.

This works fine, UNLESS the theme folder name changes.

So let's say someone downloads this new version from GitHub and keeps the -master part of the folder name and they activate it.

All of a sudden, the widget that was in the Footer 1 area is moved into the Header widget area.

My assumption would be that since we have an ID set, the order shouldn't change regardless of new widget areas being inserted into the mix.

The same things happens without the foreach as well.

Change History (3)

#1 @westonruter
11 months ago

  • Keywords reporter-feedback added

@edge22 this may in fact be a duplicate of #39693. I assume in your case that after you rename the directory, you have to then go in and re-activate the theme? In this case the issue would be happening as part of a theme switch and the issue is with the re-mapping of widgets to sidebars across themes. Please confirm.

#2 @edge22
11 months ago

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #39693.

That's correct.

Sorry about that - searched for a ticket but couldn't find one.

#3 @swissspidy
11 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.