I am currently developing a WooCommerce store, based on Astra with a lot of custom coding. I have noticed two or three times that my widgets were missing all of a sudden. In fact, they just got moved to other widget sections.
To be more specific, I have 4-column footer with 4 widgets, I have 1 widget in blog sidebar, and 4 widgets in WooCommerce sidebar.
When they got moved, they always ended up in the same widget areas. All sidebar widgets (including WC ones) were in the default blog sidebar, and 2 out of 4 footer widgets got moved to the bottom bar areas.
I don't use any plugin that does anything with widgets, and all I do with widgets in my code, is this:
1.) Allowing shortcodes in HTML/text widgets.
add_filter( 'widget_text', 'do_shortcode' );
2.) Register my custom widget (regular way, with some irrelevant markup).
function hstngr_register_widget() {
register_widget( 'brands_widget' );
}
add_action( 'widgets_init', 'hstngr_register_widget' );
class brands_widget extends WP_Widget {
function __construct() {
parent::__construct(
'brands_widget',
__('Brands widget', ' brands_widget_domain'),
array( 'description' => __( 'Display existing brands', 'brands_widget_domain' ), )
);
}
public function widget( $args, $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = apply_filters('widget_title', $instance['title']);
}
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// Output
$brands = get_terms( array(
'taxonomy' => 'vyrobca',
'hide_empty' => 0
));
echo '<ul class="product-categories sidebar-brands">';
foreach($brands as $brand) {
echo '<li class="cat-item">';
echo '<a href="' . get_term_link($brand->term_id) . '" title="' . $brand->name . '">';
echo '<img src="' . get_field('icon', 'vyrobca_' . $brand->term_id) . '" alt="' . $brand->name . '">';
echo '</a>';
echo '</li>';
}
echo '</ul>';
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) )
$title = $instance[ 'title' ];
else
$title = __( 'Brands widget', 'brands_widget_domain' );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
The widgets get moved randomly, I don't see any pattern. I do changes in the code, in the admin dashboard, install new plugins, everything is fine. Then, randomly, widgets are gone.