とりあえずメモ。
class replace_sidebar {
public function __construct() {
add_action( 'plugins_loaded', [ $this, '_bootstrap' ] );
}
public function _bootstrap() {
add_action( 'wp_head', [ $this, '_remove_sidebars' ], 11 );
add_action( 'widgets_init', [ $this, '_widgets_init' ] );
add_action( 'dynamic_sidebar_before', [ $this, '_dynamic_sidebar_before' ] );
}
public function _remove_sidebars() {
// 単純に消す場合はこう
unregister_sidebar( 'archive-sidebar-widget-area' );
add_filter( 'is_active_sidebar', function( $is_active_sidebar, $index ) {
if ( 'sidebar-widget-area' === $index ) {
return is_active_sidebar( 'new-sidebar-widget-area' );
}
return $is_active_sidebar;
}, 10, 2 );
}
public function _dynamic_sidebar_before( $index ) {
if ( 'sidebar-widget-area' === $index ) {
if ( is_active_sidebar( 'new-sidebar-widget-area' ) ) {
dynamic_sidebar( 'new-sidebar-widget-area' );
}
}
}
public function _widgets_init() {
register_sidebar( [
'name' => __( 'New sidebar', 'replace-sidebar' ),
'description' => __( 'New sidebar', 'replace-sidebar' ),
'id' => 'new-sidebar-widget-area',
] );
}
}
new replace_sidebar();