How to place random widgets in the WordPress sidebar?

Here comes the workaround solution discussed in the comments:

functions.php:

add_action( 'widgets_init', 'talfluxive_register_sidebars' );
function talfluxive_register_sidebars() {
    // register five random widget areas
    register_sidebars( 5, array( 'name' => 'Random Widget Area %d' ) );
    // register two fixed widget areas
    register_sidebars( 2, array( 'name' => 'Fixed Widget Area %d' ) );
}

sidebar.php

dynamic_sidebar( 'Fixed Widget Area 1' );
dynamic_sidebar( 'Random Widget Area ' . rand( 1, 5 ) );
dynamic_sidebar( 'Fixed Widget Area 2' );

This example code is very minimal and could be improved in many ways but it works and should serve as a good starting point.

PS: I really like the random widget idea. I will look for a better solution when I have more time. It’s a good plugin inspiration 🙂

Leave a Comment